#include <bits/stdc++.h>
using namespace std;
void gogeta() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
void swap(long long* x, long long* y) {
  long long temp = *x;
  *x = *y;
  *y = temp;
}
int32_t main() {
  gogeta();
  long long n;
  cin >> n;
  map<long long, long long> make_pair;
  long long l = 0, r = 0;
  char ch;
  cin >> ch;
  long long id;
  cin >> id;
  make_pair[0] = id;
  l = -1;
  r = 1;
  for (long long i = 1; i < n; i++) {
    char ch;
    cin >> ch;
    long long id;
    cin >> id;
    if (ch == 'L') {
      make_pair[id] = l;
      l--;
    } else if (ch == 'R') {
      make_pair[id] = r;
      r++;
    } else {
      cout << min(abs(l - make_pair[id]) - 1, abs(r - make_pair[id]) - 1)
           << endl;
    }
  }
}
