#include <bits/stdc++.h>
using namespace std;
template <typename X>
void MA(X& a, X b) {
  a = max(a, b);
}
template <typename X>
void MI(X& a, X b) {
  a = min(a, b);
}
template <typename X>
void clr(X& x, int a) {
  memset(x, a, sizeof(a));
};
int cond = 0, multi = 0, gcj = 0;
struct solver_t;
solver_t* solver;
struct solver_t {
  long long solve(set<long long>& GREEN, vector<int>& BLUE, vector<int>& RED) {
    GREEN.insert((long long)-1e18);
    GREEN.insert((long long)-1e18 - 1);
    GREEN.insert((long long)1e18);
    GREEN.insert((long long)1e18 + 1);
    long long res = 0;
    int p1 = 0;
    int p2 = 0;
    for (auto x : GREEN) {
      if (x > (long long)1e18) break;
      long long before = x;
      long long after = *(++GREEN.lower_bound(x));
      vector<long long> cands1, cands2;
      cands1.push_back(before);
      cands2.push_back(before);
      while (p1 < (int)BLUE.size()) {
        if (before <= BLUE[p1] && BLUE[p1] <= after) {
          cands1.push_back(BLUE[p1]);
          p1++;
        } else
          break;
      }
      while (p2 < (int)RED.size()) {
        if (before <= RED[p2] && RED[p2] <= after) {
          cands2.push_back(RED[p2]);
          p2++;
        } else
          break;
      }
      cands1.push_back(after);
      cands2.push_back(after);
      long long best1 = (long long)1e18, best2 = (long long)1e18;
      for (long long x : cands1) {
        if (cond)
          cerr << 64 << " "
               << "x"
               << " " << x << endl;
      };
      for (long long x : cands2) {
        if (cond)
          cerr << 65 << " "
               << "x"
               << " " << x << endl;
      };
      for (int i = 0; i < (int)cands1.size() - 1; i++) {
        best1 = min(best1, cands1[i] - before + after - cands1[i + 1]);
      }
      for (int i = 0; i < (int)cands2.size() - 1; i++) {
        best2 = min(best2, cands2[i] - before + after - cands2[i + 1]);
      }
      {
        if (cond)
          cerr << 72 << " "
               << "cands1.size() << \" \" << cands2.size() << \" \" << best1 "
                  "<< \" \" << best2"
               << " " << cands1.size() << " " << cands2.size() << " " << best1
               << " " << best2 << endl;
      };
      if (abs(after) <= 1e9 && abs(before) <= 1e9) {
        res += min(2 * (after - before), after - before + best1 + best2);
      } else
        res += best1 + best2;
      {
        if (cond)
          cerr << 77 << " "
               << "res << \" \" << after << \" \" << before"
               << " " << res << " " << after << " " << before << endl;
      }
    }
    return res;
  }
  void solve() {
    int N;
    cin >> N;
    set<long long> GREEN;
    vector<int> RED;
    vector<int> BLUE;
    for (auto i = (0); i < (N); ++i) {
      int x;
      char c;
      cin >> x >> c;
      if (c == 'G')
        GREEN.insert(x);
      else if (c == 'R')
        RED.push_back(x);
      else
        BLUE.push_back(x);
    }
    sort((RED).begin(), (RED).end());
    sort((BLUE).begin(), (BLUE).end());
    if (GREEN.size() == 0) {
      long long res = 0;
      if (RED.size()) res += *RED.rbegin() - *RED.begin();
      if (BLUE.size()) res += *BLUE.rbegin() - *BLUE.begin();
      cout << res << "\n";
      return;
    }
    long long res = 0;
    res += solve(GREEN, BLUE, RED);
    cout << res << "\n";
  }
};
void gen() {}
void brute() {}
int main(int argc, char** argv) {
  for (auto i = (1); i <= (argc - 1); ++i)
    if (argv[i] == string("q")) cond = 1 << 30;
  for (auto i = (1); i <= (argc - 1); ++i)
    if (argv[i] == string("gen")) {
      gen();
      exit(0);
    }
  for (auto i = (1); i <= (argc - 1); ++i)
    if (argv[i] == string("brute")) {
      brute();
      exit(0);
    }
  ios::sync_with_stdio(false), cin.tie(0);
  cout.setf(ios::fixed), cout.precision(10);
  int t;
  if (multi || gcj)
    cin >> t;
  else
    t = 1;
  for (auto i = (1); i <= (t); ++i) {
    if (cond) cerr << 153 << " " << i << endl;
    if (gcj) cout << "Case #" << i << ": ";
    solver = new solver_t();
    solver->solve();
  }
  return 0;
}
