#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define rep(a, b, c) for(int (a) = (b); (a) <= (c); ++ (a))
#define per(a, b, c) for(int (a) = (b); (a) >= (c); -- (a))
using namespace std;
const int N = 50010;
int n, m;
int stk[N], top;
bool in_s[N];
struct SplaY {
int tot, fa[N], ch[N][2], val[N], siz[N], cnt[N], root;
int get_node(int w) {
fa[ ++ tot] = 0;
val[tot] = w;
siz[tot] = cnt[tot] = 1;
ch[tot][0] = ch[tot][1] = 0;
return tot;
}
void upd(int u) {
siz[u] = siz[ch[u][0]] + siz[ch[u][1]] + cnt[u];
}
void rot(int x) {
int y = fa[x], z = fa[y], k = ch[y][1] == x, w = ch[x][k ^ 1];
if(z) ch[z][ch[z][1] == y] = x; fa[x] = z;
fa[fa[ch[ch[x][k ^ 1] = y][k] = w] = y] = x;
upd(y), upd(x);
}
void spl(int x, int aim) {
while(fa[x] != aim) {
int y = fa[x], z = fa[y];
if(z != aim) rot((ch[z][1] == y) ^ (ch[y][1] == x) ? x : y);
rot(x);
}
if(!aim) root = x;
}
void ins(int w) {
int u = root, y;
while(u && val[u] != w) y = u, u = ch[u][w > val[u]];
if(u) ++ cnt[u];
else {
u = get_node(w);
if(y) ch[y][w > val[y]] = u;
fa[u] = y;
}
spl(u, 0);
}
int getu(int w) {
int u = root;
while(val[u] != w && ch[u][w > val[u]]) u = ch[u][w > val[u]];
return u;
}
int get_pr(int w) {
int u = getu(w);
if(val[u] < w) return u;
spl(u, 0);
u = ch[u][0];
while(ch[u][1]) u = ch[u][1];
return u;
}
int get_nt(int w) {
int u = getu(w);
if(val[u] > w) return u;
spl(u, 0);
u = ch[u][1];
while(ch[u][0]) u = ch[u][0];
return u;
}
void del(int w) {
int pr = get_pr(w), nt = get_nt(w);
spl(pr, 0), spl(nt, pr);
int u = ch[nt][0];
if(cnt[u] > 1) -- cnt[u], upd(u);
else ch[nt][0] = 0;
upd(nt), upd(pr);
}
}SP;
int main() {
scanf("%d%d", &n, &m);
SP.ins(0), SP.ins(n + 1);
while(m -- ) {
char op[2];
scanf("%s", op);
if(*op == 'D') {
int x;
scanf("%d", &x);
SP.ins(x);
stk[ ++ top] = x;
in_s[x] = true;
}
else if(*op == 'R') {
int x = stk[top -- ];
SP.del(x);
in_s[x] = false;
}
else {
int x;
scanf("%d", &x);
if(in_s[x]) puts("0");
else printf("%d\n", SP.val[SP.get_nt(x)] - SP.val[SP.get_pr(x)] - 1);
}
}
return 0;
}