#include <bits/stdc++.h>
using namespace std;
deque<long long int> v;
long long int a[(int)6e5];
int main() {
  long long int i, j, k, n, m, q, l = 3e5, r = 3e5;
  char ch;
  cin >> q;
  while (q--) {
    cin >> ch >> n;
    if (ch == 'L') {
      a[n] = l - 1;
      l--;
      v.push_front(a[n]);
    } else if (ch == 'R') {
      a[n] = r + 1;
      r++;
      v.push_back(a[n]);
    } else {
      long long int x = a[n];
      long long int le = 0, ri = v.size() - 1;
      long long int ans = 0;
      while (le <= ri) {
        long long int mid = (le + ri) / 2;
        if (v[mid] == x) {
          ans = mid;
          break;
        } else if (x < v[mid])
          ri = mid - 1;
        else
          le = mid + 1;
      }
      cout << min(ans, (long long int)v.size() - 1 - ans) << '\n';
    }
  }
}
