#include <bits/stdc++.h>
using namespace std;
const int MaxN = 200000 + 5, MaxV = 400000 + 5;
int Q;
int id[MaxN];
int Bit[MaxV];
int pos_left, pos_right;
inline int lowbit(int x) { return x & -x; }
inline void update(int x, int v) {
  for (int i = x; i <= Q * 2; i += lowbit(i)) Bit[i] += v;
}
inline int query(int x) {
  int res = 0;
  for (int i = x; i > 0; i -= lowbit(i)) res += Bit[i];
  return res;
}
int main() {
  cin >> Q;
  pos_left = Q + 1, pos_right = Q + 1;
  for (int i = 1; i <= Q; ++i) {
    char c;
    int v;
    scanf(" %c %d", &c, &v);
    if (c == 'L') {
      id[v] = --pos_left;
      update(id[v], 1);
    } else if (c == 'R') {
      id[v] = pos_right++;
      update(id[v], 1);
    } else {
      int l = query(id[v]) - 1;
      printf("%d\n", min(l, pos_right - pos_left - l - 1));
    }
  }
  return 0;
}
