#include <bits/stdc++.h>
using namespace std;
int II() {
  int q;
  scanf("%d", &q);
  return q;
}
void solve() {
  int n;
  cin >> n;
  map<int, int> mp;
  int cnt = 0;
  int l = 0, r = 1;
  while (n--) {
    char c;
    cin >> c;
    if (c == 'L') {
      int in;
      cin >> in;
      mp[in] = l--;
    } else if (c == 'R') {
      int in;
      cin >> in;
      mp[in] = r++;
    } else {
      int in;
      cin >> in;
      int qq = abs(mp[in] - l);
      int qqq = abs(mp[in] - r);
      cout << min(qq, qqq) - 1 << endl;
    }
  }
}
int main() {
  int tt;
  tt = 1;
  int cas = 1;
  while (tt--) {
    solve();
  }
}
