#include<iostream>#include<cstdio>#include<set>#include<stack>usingnamespace std;intmain(){
int n,m;while(scanf("%d%d",&n,&m)!=EOF){
set<int> se={
0,n+1};stack<int> stk;while(m--){
char op[2];scanf("%s",op);if(*op=='D'){
int x;scanf("%d",&x);se.insert(x);stk.push(x);}if(*op=='Q'){
int x;scanf("%d",&x);int res =*se.upper_bound(x)-*(--se.upper_bound(x))-1;if(se.count(x)) res=0;printf("%d\n",res);}if(*op=='R'&&!stk.empty()) se.erase(stk.top()),stk.pop();}}return0;}