#include <bits/stdc++.h>
#define ll long long
#define _for(i, a, b) for (int i = (a); i <= (b); i ++ )
#define _all(i, a, b) for (int i = (a); i >= (b); i -- )
using namespace std;
int n, m;
multiset<int> s;
multiset<int> :: iterator it;
stack<int> d;
int main() {
ios :: sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> m;
s.insert(0), s.insert(n + 1);
char opt;
int x;
while (m -- ) {
cin >> opt;
if (opt == 'D') {
cin >> x;
s.insert(x), d.push(x);
}
else if (opt == 'R') s.erase(s.find(d.top())), d.pop();
else {
cin >> x;
it = s.lower_bound(x);
if (*it == x) puts("0");
else cout << (*it - *( -- it)) - 1 << endl;
}
}
return 0;
}