#include <bits/stdc++.h>
using namespace std;
int i, j, m, n, a[300005], b[300005], c[300005], temp, cnt1, cnt2, cnt3, pt1,
    pt2, pt3;
long long ans, inf, leftmin, rightmax, gapmax, min1, min2;
bool flag;
char ch;
long long getmin(long long a, long long b) {
  if (a < b)
    return a;
  else
    return b;
}
long long getmax(long long a, long long b) {
  if (a > b)
    return a;
  else
    return b;
}
int main() {
  inf = 1100000009;
  scanf("%d", &n);
  cnt1 = cnt2 = cnt3 = 0;
  for (i = 1; i <= n; i++) {
    scanf("%d", &temp);
    scanf("%c", &ch);
    scanf("%c", &ch);
    if (ch == 'G') {
      cnt1++;
      a[cnt1] = temp;
    } else if (ch == 'R') {
      cnt2++;
      b[cnt2] = temp;
    } else if (ch == 'B') {
      cnt3++;
      c[cnt3] = temp;
    }
  }
  sort(a + 1, a + cnt1 + 1);
  sort(b + 1, b + cnt2 + 1);
  sort(c + 1, c + cnt3 + 1);
  pt1 = pt2 = pt3 = 1;
  ans = 0;
  if (cnt1 == 0) {
    ans = b[cnt2] - b[1] + c[cnt3] - c[1];
    printf("%lld\n", ans);
  } else {
    if ((cnt2 > 0) && (b[1] < a[1])) ans += a[1] - b[1];
    if ((cnt3 > 0) && (c[1] < a[1])) ans += a[1] - c[1];
    if ((cnt2 > 0) && (b[cnt2] > a[cnt1])) ans += b[cnt2] - a[cnt1];
    if ((cnt3 > 0) && (c[cnt3] > a[cnt1])) ans += c[cnt3] - a[cnt1];
    while ((b[pt2] <= a[1]) && (pt2 <= cnt2)) pt2++;
    while ((c[pt3] <= a[1]) && (pt3 <= cnt3)) pt3++;
    for (i = 1; i <= cnt1 - 1; i++) {
      leftmin = inf;
      rightmax = 0;
      gapmax = 0;
      flag = false;
      while ((b[pt2] <= a[i + 1]) && (pt2 <= cnt2)) {
        flag = true;
        leftmin = getmin(leftmin, b[pt2]);
        rightmax = getmax(rightmax, b[pt2]);
        if ((pt2 > 1) && (b[pt2 - 1] > a[i])) {
          gapmax = getmax(gapmax, b[pt2] - b[pt2 - 1]);
        }
        pt2++;
      }
      if (flag)
        min1 = getmin(getmin(a[i + 1] - leftmin, rightmax - a[i]),
                      a[i + 1] - a[i] - gapmax);
      else
        min1 = 0;
      leftmin = inf;
      rightmax = 0;
      gapmax = 0;
      flag = false;
      while ((c[pt3] <= a[i + 1]) && (pt3 <= cnt3)) {
        flag = true;
        leftmin = getmin(leftmin, c[pt3]);
        rightmax = getmax(rightmax, c[pt3]);
        if ((pt3 > 1) && (c[pt3 - 1] > a[i])) {
          gapmax = getmax(gapmax, c[pt3] - c[pt3 - 1]);
        }
        pt3++;
      }
      if (flag)
        min2 = getmin(getmin(a[i + 1] - leftmin, rightmax - a[i]),
                      a[i + 1] - a[i] - gapmax);
      else
        min2 = 0;
      if (min1 + min2 < a[i + 1] - a[i]) {
        ans += min1 + min2 + a[i + 1] - a[i];
      } else {
        ans += (a[i + 1] - a[i]) * 2;
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
