#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
void chmin(T1 &a, T2 b) {
  if (a > b) a = b;
}
template <class T1, class T2>
void chmax(T1 &a, T2 b) {
  if (a < b) a = b;
}
using Pi = pair<long long, long long>;
using Ti = tuple<long long, long long, long long>;
using vint = vector<long long>;
const long long inf = 1LL << 55;
const long long mod = 1e9 + 7;
struct UnionFind {
  vector<long long> data;
  UnionFind() {}
  UnionFind(long long sz) : data(sz, -1){};
  long long size(long long x) { return -data[find(x)]; }
  long long find(long long x) {
    if (data[x] < 0) return x;
    return data[x] = find(data[x]);
  }
  bool same(long long x, long long y) { return find(x) == find(y); }
  void unite(long long x, long long y) {
    x = find(x), y = find(y);
    if (x == y) return;
    data[x] += data[y];
    data[y] = x;
    return;
  }
};
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);
  long long n;
  cin >> n;
  vint p(n);
  vector<char> c(n);
  for (long long i = (long long)(0); i < (long long)(n); i++)
    cin >> p[i] >> c[i];
  long long redcnt = 0, bluecnt = 0, greencnt = 0;
  long long onlyred = 0, onlyblue = 0, onlygreen = 0;
  vector<vint> graph(n);
  UnionFind uf(n);
  vint greencost(n, -1);
  long long pr = -1, pb = -1, pg = -1;
  priority_queue<Pi, vector<Pi>, greater<Pi>> que;
  for (long long i = (long long)(0); i < (long long)(n); i++) {
    if (c[i] == 'R') {
      redcnt++;
      if (~pr) {
        onlyred += p[i] - p[pr];
        graph[pr].push_back(i);
        graph[i].push_back(pr);
      }
      if (~pg) uf.unite(pg, i);
      if (pr < pg) graph[pg].push_back(i);
      pr = i;
    } else if (c[i] == 'B') {
      bluecnt++;
      if (~pb) {
        onlyblue += p[i] - p[pb];
        graph[pb].push_back(i);
        graph[i].push_back(pb);
      }
      if (~pg) uf.unite(pg, i);
      if (pb < pg) graph[pg].push_back(i);
      pb = i;
    } else if (c[i] == 'G') {
      greencnt++;
      if (~pg) {
        onlygreen += p[i] - p[pg];
        greencost[pg] = p[i] - p[pg];
      }
      if (pg < pr) graph[i].push_back(pr);
      if (pg < pb) graph[i].push_back(pb);
      pg = i;
      que.emplace(0, i);
    }
  }
  if (redcnt == 0 && bluecnt == 0) {
    cout << onlygreen << endl;
    return 0;
  }
  if (greencnt == 0) {
    cout << onlyred + onlyblue << endl;
    return 0;
  }
  long long ans = onlygreen;
  vint sumch(n, 0);
  vint used(n, 0);
  while (!que.empty()) {
    Pi pp = que.top();
    que.pop();
    long long v = pp.second;
    if (used[v]) continue;
    used[v] = 1;
    ans += pp.first;
    sumch[uf.find(v)] += pp.first;
    for (long long u : graph[v]) {
      if (used[u]) continue;
      que.emplace(llabs(p[v] - p[u]), u);
    }
  }
  for (long long i = (long long)(0); i < (long long)(n); i++) {
    if (c[i] != 'G' || greencost[i] == -1) continue;
    if (greencost[i] < sumch[i]) {
      ans -= sumch[i];
      ans += greencost[i];
    }
  }
  cout << ans << endl;
  return 0;
}
