#include <bits/stdc++.h>
using namespace std;
const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const int inf = 0x3f3f3f3f;
const long long linf = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1);
const double EPS = 1e-7;
const long long mod = 1e9 + 7;
const long long mod2 = 1e9 + 9;
long long mypow(long long x, long long y, long long p) {
  long long ret = 1;
  while (y) {
    if (y & 1) ret = (ret * x) % p;
    x = (x * x) % p;
    y /= 2;
  }
  return ret;
}
long long inv(long long x, long long p) { return mypow(x, p - 2, p); }
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
const int MAX = 6e5 + 3;
const int MID = 3e5;
int Q, lis[MAX], l = MID, r = MID + 1, id[MAX];
int main() {
  scanf("%d", &Q);
  for (int i = 0; i < Q; i++) {
    char c;
    int x;
    scanf(" %c %d", &c, &x);
    if (c == 'L') {
      lis[l--] = x;
      id[x] = l + 1;
    } else if (c == 'R') {
      lis[r++] = x;
      id[x] = r - 1;
    } else {
      int sz = r - l;
      int idx = id[x];
      int ans = 1e9;
      ans = min(ans, idx - l);
      ans = min(ans, r - idx);
      printf("%d\n", ans - 1);
    }
  }
  return 0;
}
