#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const long long MAXN = 2e5 + 100;
const long long MOD = 1000000007;
struct ele {
  long long pos, ty;
  bool operator<(const ele &a) const { return pos < a.pos; }
} ax[MAXN];
int main(int argc, char const *argv[]) {
  long long a[2], b[2], c[2];
  a[0] = b[0] = c[0] = INF * 1;
  a[1] = b[1] = c[1] = INF * -1;
  long long n, idx = 0;
  scanf("%lld", &n);
  for (int i = 0; i < n; ++i) {
    char type[3];
    long long pos;
    scanf("%lld %s", &pos, type);
    ax[idx].pos = pos;
    if (type[0] == 'B') {
      a[0] = min(a[0], pos);
      a[1] = max(a[1], pos);
      ax[idx++].ty = 0;
    } else if (type[0] == 'R') {
      b[0] = min(b[0], pos);
      b[1] = max(b[1], pos);
      ax[idx++].ty = 1;
    } else {
      c[0] = min(c[0], pos);
      c[1] = max(c[1], pos);
      ax[idx++].ty = 2;
    }
  }
  long long ans = 0;
  bool sp = 0;
  if (c[0] == INF) {
    sp = 1;
    ans = max(0ll, a[1] - a[0ll]) + max(0ll, b[1] - b[0ll]);
  }
  if (sp) {
    cout << ans << endl;
    return 0;
  }
  ans += max(0ll, c[0ll] - a[0ll]) + max(0ll, c[0ll] - b[0ll]) +
         max(0ll, a[1] - c[1]) + max(0ll, b[1] - c[1]);
  long long last[2];
  long long lastc;
  long long bg[2];
  sort(ax, ax + n);
  for (int i = 0; i < n; ++i) {
    if (ax[i].pos < c[0]) continue;
    if (ax[i].pos > c[1]) {
      break;
    }
    if (ax[i].pos == c[0]) {
      last[0] = last[1] = lastc = i;
      bg[0] = bg[1] = 0;
    }
    for (int j = 0; j < 2; ++j) bg[j] = max(bg[j], ax[i].pos - ax[last[j]].pos);
    if (ax[i].ty < 2) last[ax[i].ty] = i;
    if (ax[i].ty == 2) {
      long long len = ax[i].pos - ax[lastc].pos;
      ans += min(len * 2, len * 3 - bg[0] - bg[1]);
      last[0] = last[1] = lastc = i;
      bg[0] = bg[1] = 0;
    }
  }
  cout << ans << endl;
  return 0;
}
