#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const long double eps = 1e-9;
vector<long long int> getVec(vector<pair<long long int, int> > points, int fl) {
  vector<long long int> vec;
  for (int i = 0; i < points.size(); i++) {
    if (points[i].second == fl) vec.push_back(points[i].first);
  }
  return vec;
}
long long int cost1(long long int st, long long int en,
                    vector<long long int> p) {
  long long int mx = max(p[0] - st, en - p.back());
  for (int i = 1; i < p.size(); i++) {
    mx = max(p[i] - p[i - 1], mx);
  }
  return mx;
}
long long int cost(long long int st, long long int en,
                   vector<pair<long long int, int> > points) {
  if (points.size() == 0) return en - st;
  int fl = 1;
  for (int i = 1; i < (int)points.size(); i++) {
    if (points[i].second != points[i - 1].second) fl = 0;
  }
  if (fl) {
    vector<long long int> v = getVec(points, points[0].second);
    return 2LL * (en - st) - cost1(st, en, v);
  }
  long long int ans = 2LL * (en - st);
  vector<long long int> v1 = getVec(points, 1), v2 = getVec(points, 2);
  ans = min(ans, 3 * (en - st) - cost1(st, en, v1) - cost1(st, en, v2));
  return ans;
}
int main() {
  int n;
  scanf("%d", &n);
  vector<pair<long long int, int> > points;
  for (int i = 1; i <= n; i++) {
    char s[5];
    int p;
    scanf("%d %s", &p, s);
    if (s[0] == 'R') points.push_back(make_pair(p, 1));
    if (s[0] == 'B') points.push_back(make_pair(p, 2));
    if (s[0] == 'G') points.push_back(make_pair(p, 0));
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (points[i].second == 0) cnt++;
  }
  if (cnt == 0) {
    long long int mnp1 = -1, mxp1 = -1, mnp2 = -1, mxp2 = -1;
    for (int i = 0; i < n; i++) {
      if (points[i].second == 1)
        mxp1 = points[i].first;
      else
        mxp2 = points[i].first;
    }
    for (int i = n - 1; i >= 0; i--) {
      if (points[i].second == 1)
        mnp1 = points[i].first;
      else
        mnp2 = points[i].first;
    }
    printf("%lld\n", mxp1 - mnp1 + mxp2 - mnp2);
    return 0;
  }
  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    if (points[i].second != 0) continue;
    long long int mnp1 = points[i].first, mnp2 = points[i].first;
    for (int j = i - 1; j >= 0; j--) {
      if (points[j].second == 1)
        mnp1 = points[j].first;
      else
        mnp2 = points[j].first;
    }
    ans += (points[i].first - mnp1 + points[i].first - mnp2);
    break;
  }
  for (int i = n - 1; i >= 0; i--) {
    if (points[i].second != 0) continue;
    long long int mxp1 = points[i].first, mxp2 = points[i].first;
    for (int j = i + 1; j < n; j++) {
      if (points[j].second == 1)
        mxp1 = points[j].first;
      else
        mxp2 = points[j].first;
    }
    ans += (mxp1 - points[i].first + mxp2 - points[i].first);
    break;
  }
  for (int i = 0; i + 1 < n; i++) {
    if (points[i].second != 0) continue;
    int l = i, r = i + 1;
    while (r + 1 < n && points[r].second != 0) r++;
    if (points[r].second != 0) break;
    vector<pair<long long int, int> > p;
    for (int j = l + 1; j < r; j++) {
      p.push_back(points[j]);
    }
    ans += cost(points[l].first, points[r].first, p);
    i = r - 1;
  }
  printf("%lld\n", ans);
  return 0;
}
