#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> a, b, c;
long long ans = 0;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int d;
    char x;
    scanf("%d %c", &d, &x);
    if (x == 'B') {
      a.push_back(d);
    } else if (x == 'R') {
      b.push_back(d);
    } else {
      a.push_back(d);
      b.push_back(d);
      c.push_back(d);
    }
  }
  if (a.size()) ans += a[a.size() - 1] - a[0];
  if (b.size()) ans += b[b.size() - 1] - b[0];
  int l = 0, r = 1, id1 = 0, id2 = 0;
  while (r < c.size()) {
    while (id1 < a.size() && a[id1] <= c[l]) id1++;
    while (id2 < b.size() && b[id2] <= c[l]) id2++;
    long long mx1 = 0, mx2 = 0;
    while (id1 < a.size() && a[id1] <= c[r]) {
      mx1 = max(mx1, (long long)a[id1] - a[id1 - 1]);
      id1++;
    }
    while (id2 < b.size() && b[id2] <= c[r]) {
      mx2 = max(mx2, (long long)b[id2] - b[id2 - 1]);
      id2++;
    }
    if (mx1 + mx2 > c[r] - c[l]) {
      ans -= mx1 + mx2 + c[l] - c[r];
    }
    l++;
    r++;
  }
  printf("%lld\n", ans);
  return 0;
}
