#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  scanf("%d", &n);
  int k;
  char c;
  scanf("%c %d", &c, &k);
  vector<int> l, r;
  int ln = n;
  l.reserve(ln);
  r.reserve(ln);
  vector<char> loc(2000000);
  vector<int> num(2000000);
  for (int i = 0; i < n; i++) {
    cin >> c;
    if (c == 'L') {
      int b;
      scanf("%d", &b);
      l.push_back(b);
      loc[b] = 'L';
      num[b] = l.size() - 1;
    } else if (c == 'R') {
      int b;
      scanf("%d", &b);
      r.push_back(b);
      loc[b] = 'R';
      num[b] = r.size() - 1;
    } else {
      int b;
      scanf("%d", &b);
      if (loc[b] == 'L') {
        int uf = l.size() - num[b];
        int f = num[b] + r.size() + 1;
        printf("%d\n", min(uf - 1, f - 1));
        continue;
      }
      if (loc[b] == 'R') {
        int uf = r.size() - num[b];
        int f = num[b] + l.size() + 1;
        printf("%d\n", min(uf - 1, f - 1));
        continue;
      }
      int f = min(l.size(), r.size());
      printf("%d\n", f);
    }
  }
  return 0;
}
