#include <bits/stdc++.h>
using namespace std;
vector<long long> r, g, b;
int main(void) {
  int n;
  while (~scanf("%d", &n)) {
    r.clear(), b.clear(), g.clear();
    for (int i = 0; i < n; ++i) {
      long long p;
      char c[20];
      scanf("%lld%s", &p, c);
      switch (c[0]) {
        case 'R':
          r.push_back(p);
          break;
        case 'G':
          g.push_back(p);
          break;
        case 'B':
          b.push_back(p);
          break;
      }
    }
    long long ans = 0;
    if (g.empty()) {
      if (!r.empty()) ans += r.back() - r.front();
      if (!b.empty()) ans += b.back() - b.front();
    } else {
      int rl, rr, bl, br;
      int R = r.size(), B = b.size(), G = g.size();
      rl = bl = 0;
      rr = br = 0;
      while (rr < R && r[rr] < g[0]) ++rr;
      while (br < B && b[br] < g[0]) ++br;
      if (rr > 0) ans += g[0] - r[0];
      if (br > 0) ans += g[0] - b[0];
      for (int i = 0; i < G - 1; ++i) {
        rl = rr, bl = br;
        while (rr < R && r[rr] < g[i + 1]) ++rr;
        while (br < B && b[br] < g[i + 1]) ++br;
        long long tmpr = 0, tmpb = 0;
        long long tmp = g[i + 1] - g[i];
        if (rl < rr) {
          tmpr = r[rl] - g[i];
          for (int j = rl; j < rr; ++j)
            tmpr = max(tmpr, (j == rr - 1 ? g[i + 1] : r[j + 1]) - r[j]);
        } else
          tmpr = tmp;
        if (bl < br) {
          tmpb = b[bl] - g[i];
          for (int j = bl; j < br; ++j)
            tmpb = max(tmpb, (j == br - 1 ? g[i + 1] : b[j + 1]) - b[j]);
        } else
          tmpb = tmp;
        ans += min(3 * tmp - tmpr - tmpb, rl < rr || bl < br ? 2 * tmp : tmp);
      }
      rl = rr, bl = br;
      rr = R, br = B;
      if (rr > rl) ans += r.back() - g.back();
      if (br > bl) ans += b.back() - g.back();
    }
    printf("%lld\n", ans);
  }
  return 0;
}
