rt
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
struct TREE {
int ch[2], p, v;
}tr[N];
stack<int> s;
int n, m, root, idx;
void rotate(int u) {
int x = tr[u].p, y = tr[x].p;
int k = tr[x].ch[1] == u;
tr[y].ch[tr[y].ch[1] == x] = u, tr[u].p = y;
tr[x].ch[k] = tr[u].ch[k ^ 1], tr[tr[u].ch[k ^ 1]].p = x;
tr[u].ch[k ^ 1] = x, tr[x].p = u;
}
void splay(int u, int k) {
while (tr[u].p != k) {
int x = tr[u].p, y = tr[x].p;
if (y != k) {
if ((tr[y].ch[1] == x) ^ (tr[x].ch[1] == u)) rotate(u);
else rotate(x);
}
rotate(u);
}
if (!k) root = u;
}
int get_max(int u) {
int res;
while (u) {
res = u;
u = tr[u].ch[1];
}
return res;
}
void D(int u) {
splay(u, 0);
if (!tr[u].ch[0]) root = tr[u].ch[1], tr[tr[u].ch[1]].p = 0;
else if (!tr[u].ch[1]) root = tr[u].ch[0], tr[tr[u].ch[0]].p = 0;
else {
splay(get_max(tr[u].ch[0]), u);
root = tr[u].ch[0];
tr[tr[u].ch[1]].p = tr[u].ch[0];
tr[tr[u].ch[0]].ch[1] = tr[u].ch[1];
}
}
int get_k(int v) {
int u = root;
while (u) {
if (tr[u].v == v) return u;
u = tr[u].ch[tr[u].v < v];
}
}
int Ne(int k) {
int u = root, res = 0;
while (u) {
if (tr[u].v >= k) res = u, u = tr[u].ch[0];
else u = tr[u].ch[1];
}
return tr[res].v;
}
int Pr(int k) {
int u = root, res = 0;
while (u) {
if (tr[u].v <= k) res = u, u = tr[u].ch[1];
else u = tr[u].ch[0];
}
return tr[res].v;
}
void insert(int v) {
int u = root, p = 0;
while (u) p = u, u = tr[u].ch[v > tr[u].v];
u = ++ idx;
if (p) tr[p].ch[v > tr[p].v] = u;
tr[u].p = p, tr[u].v = v;
splay(u, 0);
}
int main() {
cin >> n >> m;
insert(0), insert(n + 1);
stack<int> S;
for (int i = 1; i <= m; i ++ ) {
char s[3];
int k;
scanf("%s", s);
if (s[0] == 'D') {
scanf("%d", &k);
insert(k);
S.push(k);
}
else if (s[0] == 'R') {
D(get_k(S.top()));
S.pop();
}
else {
scanf("%d", &k);
int a = Ne(k), b = Pr(k);
if (a == k || b == k) printf("0\n");
else printf("%d\n", a - b - 1);
}
}
return 0;
}