#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
const long long inf = 0x3f3f3f3f3f3f3f3f;
const long long mod = 1e9 + 7;
const long double eps = 0.000001;
using namespace std;
pair<int, int> num[maxn];
vector<int> pp[4];
int main() {
  int n;
  scanf("%d", &n);
  int mi = 1e9 + 10, ma = -mi, flag1 = 0, flag2 = 0;
  for (int i = 1; i <= n; i++) {
    int p, p1;
    char str[2];
    scanf("%d%s", &p, str);
    if (str[0] == 'B')
      p1 = 1, flag1 = 1;
    else if (str[0] == 'R')
      p1 = 2, flag2 = 1;
    else
      p1 = 3;
    pp[p1].push_back(p);
    num[i] = pair<int, int>(p, p1);
  }
  if (flag1 + flag2 < 1) {
    printf("%d\n", num[n].first - num[1].first);
    return 0;
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (num[i].second != 1)
      mi = min(mi, num[i].first), ma = max(ma, num[i].first);
  }
  if (mi != 1e9 + 10) ans = ma - mi;
  mi = 1e9 + 10, ma = -mi;
  for (int i = 1; i <= n; i++) {
    if (num[i].second != 2)
      mi = min(mi, num[i].first), ma = max(ma, num[i].first);
  }
  if (mi != 1e9 + 10) ans += ma - mi;
  for (int i = 1; i < pp[3].size(); i++) {
    int l = pp[3][i - 1], r = pp[3][i], nn1 = 0, nn2 = 0;
    ans -= (r - l) * 2;
    int p1 = upper_bound(pp[1].begin(), pp[1].end(), l) - pp[1].begin();
    if (p1 == pp[1].size() || pp[1][p1] > r)
      nn1 = 0;
    else {
      nn1 = pp[1][p1++] - l;
      while (p1 < pp[1].size() && pp[1][p1] <= r) {
        nn1 = max(nn1, pp[1][p1] - pp[1][p1 - 1]);
        p1++;
      }
      nn1 = max(nn1, r - pp[1][p1 - 1]);
    }
    int p2 = upper_bound(pp[2].begin(), pp[2].end(), l) - pp[2].begin();
    if (p2 == pp[2].size() || pp[2][p2] > r)
      nn2 = 0;
    else {
      nn2 = pp[2][p2++] - l;
      while (p2 < pp[2].size() && pp[2][p2] <= r) {
        nn2 = max(nn2, pp[2][p2] - pp[2][p2 - 1]);
        p2++;
      }
      nn2 = max(nn2, r - pp[2][p2 - 1]);
    }
    long long ss = r - l;
    if (nn1) ss += r - l - nn1;
    if (nn2) ss += r - l - nn2;
    ans += min((long long)(r - l) * 2, ss);
  }
  cout << ans << endl;
}
