#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 9;
const int inf = (1 << 30);
int n, m, ans;
struct Splay {
int son[2], fa, val, sz, cnt;
void init(int _v, int _fa) {
val = _v, fa = _fa;
sz = 1, cnt = 1;
}
} tr[MAXN];
int root, tot;
void push_up(int o) {
tr[o].sz = tr[tr[o].son[0]].sz + tr[tr[o].son[1]].sz + tr[o].cnt;
}
void rotate(int x) {
int y = tr[x].fa, z = tr[y].fa;
int d = x == tr[y].son[1];
tr[z].son[y == tr[z].son[1]] = x, tr[x].fa = z;
tr[y].son[d] = tr[x].son[d ^ 1], tr[tr[x].son[d ^ 1]].fa = y;
tr[x].son[d ^ 1] = y, tr[y].fa = x;
push_up(y), push_up(x);
}
void splay(int x, int k) {
while (tr[x].fa != k) {
int y = tr[x].fa, z = tr[y].fa;
if (z != k) (x == tr[y].son[0]) ^ (y == tr[z].son[0]) ? rotate(x) : rotate(y);
rotate(x);
}
if (!k) root = x;
}
void insert(int v) {
if (v < m) return;
int o = root, fa = 0;
while (o && v != tr[o].val) {
fa = o;
o = tr[o].son[v > tr[o].val];
}
if (o) tr[o].cnt++;
else {
o = ++tot;
if (fa) tr[fa].son[v > tr[fa].val] = o;
tr[o].init(v, fa);
}
splay(o, 0);
}
void add(int v) {
for (int i = 0; i < tot; i++)
tr[i].val += v;
}
void sub(int v) {
for (int i = 0; i < tot; i++)
tr[i].val -= v;
}
void get_pre(int v) {
int o = root;
while (tr[o].val != v && tr[o].son[v > tr[o].val]) o = tr[o].son[v > tr[o].val];
splay(o, 0);
}
int get_nxt(int v) {
get_pre(v);
if (tr[root].val >= v) return root;
int o = tr[root].son[1];
while (tr[o].son[0]) o = tr[o].son[0];
return o;
}
void remove(int v) {
int o = get_nxt(v + m);
splay(o, 0);
ans += tr[tr[o].son[0]].sz;
tr[o].son[0] = 0;
push_up(o);
sub(v);
}
int find(int k) {
if (k >= tr[root].sz) return -1;
int o = root;
while (1) {
if (tr[o].son[1] && k <= tr[tr[o].son[1]].sz) o = tr[o].son[1];
else if (k > tr[tr[o].son[1]].sz + tr[o].cnt) {
k = k - tr[tr[o].son[1]].sz - tr[o].cnt;
o = tr[o].son[0];
} else return tr[o].val;
}
}
int main() {
cin >> n >> m;
while (n--) {
char op;
int k;
cin >> op >> k;
if (op == 'I') insert(k);
if (op == 'A') add(k);
if (op == 'S') remove(k);
if (op == 'F') cout << find(k) << endl;
}
cout << ans << endl;
return 0;
}```