#include <bits/stdc++.h>
using namespace std;
int cnt, poz[250000], d[500005];
deque<int> numere;
int main() {
  char c;
  int n, i, cate, val, stanga = 250000, dreapta = 250001;
  ios_base::sync_with_stdio();
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> c;
    if (c == 'L') {
      cin >> val;
      d[stanga--] = val;
      poz[val] = stanga + 1;
    } else if (c == 'R') {
      cin >> val;
      d[dreapta++] = val;
      poz[val] = dreapta - 1;
    } else {
      cin >> val;
      cout << min(dreapta - poz[val], poz[val] - stanga) - 1 << "\n";
    }
  }
  return 0;
}
