#include <bits/stdc++.h>
using namespace std;
long long readi() {
  long long input = 0;
  char c = ' ';
  while (c < '-') {
    c = getchar();
  }
  bool negative = false;
  if (c == '-') {
    negative = true;
    c = getchar();
  }
  while (c >= '0') {
    input = 10 * input + (c - '0');
    c = getchar();
  }
  if (negative) {
    input = -input;
  }
  return input;
}
void printi(long long output) {
  if (output == 0) {
    putchar('0');
    return;
  }
  if (output < 0) {
    putchar('-');
    output = -output;
  }
  int aout[20];
  int ilen = 0;
  while (output) {
    aout[ilen] = ((output % 10));
    output /= 10;
    ilen++;
  }
  for (int i = ilen - 1; i >= 0; i--) {
    putchar(aout[i] + '0');
  }
  return;
}
string reads() {
  string input = "";
  char c = ' ';
  while (c <= ' ') {
    c = getchar();
  }
  while (c > ' ') {
    input += c;
    c = getchar();
  }
  return input;
}
int sgn(long long x) {
  if (x < 0) {
    return -1;
  }
  if (x > 0) {
    return 1;
  }
  return 0;
}
long long randomize(long long mod) {
  return ((1 << 30) * rand() + (1 << 15) * rand() + rand()) % mod;
}
const long double PI = 4.0 * atan(1.0);
const long double EPS = 1e-30;
long long normalize(long long x, long long mod) {
  return (((x % mod) + mod) % mod);
}
int N;
vector<long long> a, both, b;
long long ans;
int32_t main() {
  ios_base::sync_with_stdio(0);
  srand(time(0));
  if (fopen("cf908f.in", "r")) {
    freopen("cf908f.in", "r", stdin);
    freopen("cf908f.out", "w", stdout);
  }
  cin >> N;
  for (int i = 0; i < N; i++) {
    long long pos;
    char color;
    cin >> pos >> color;
    if (color == 'R') {
      a.push_back(pos);
    }
    if (color == 'G') {
      both.push_back(pos);
    }
    if (color == 'B') {
      b.push_back(pos);
    }
  }
  if (both.empty()) {
    if (!b.empty()) {
      ans += b.back() - b[0];
    }
    if (!a.empty()) {
      ans += a.back() - a[0];
    }
    cout << ans << '\n';
    return 0;
  }
  if (!a.empty() && a.back() > both.back()) {
    ans += (a.back() - both.back());
    while (!a.empty() && a.back() > both.back()) {
      a.pop_back();
    }
  }
  if (!b.empty() && b.back() > both.back()) {
    ans += (b.back() - both.back());
    while (!b.empty() && b.back() > both.back()) {
      b.pop_back();
    }
  }
  reverse(a.begin(), a.end());
  reverse(b.begin(), b.end());
  reverse(both.begin(), both.end());
  if (!a.empty() && a.back() < both.back()) {
    ans += (both.back() - a.back());
    while (!a.empty() && a.back() < both.back()) {
      a.pop_back();
    }
  }
  if (!b.empty() && b.back() < both.back()) {
    ans += (both.back() - b.back());
    while (!b.empty() && b.back() < both.back()) {
      b.pop_back();
    }
  }
  reverse(a.begin(), a.end());
  reverse(b.begin(), b.end());
  reverse(both.begin(), both.end());
  while (both.size() >= 2) {
    long long lt = both[both.size() - 2], rt = both[both.size() - 1];
    long long sz = rt - lt;
    long long cn = sz, dcn = 0;
    bool ina = false, inb = false;
    if (!a.empty() && a.back() > lt) {
      ina = true;
      long long ha = 0;
      ha = max(ha, rt - a.back());
      while (a.size() >= 2 && a[a.size() - 2] > lt) {
        ha = max(ha, a[a.size() - 1] - a[a.size() - 2]);
        a.pop_back();
      }
      ha = max(ha, a.back() - lt);
      cn += (sz - ha);
      dcn += sz;
      a.pop_back();
    }
    if (!b.empty() && b.back() > lt) {
      inb = true;
      long long hb = 0;
      hb = max(hb, rt - b.back());
      while (b.size() >= 2 && b[b.size() - 2] > lt) {
        hb = max(hb, b[b.size() - 1] - b[b.size() - 2]);
        b.pop_back();
      }
      hb = max(hb, b.back() - lt);
      cn += (sz - hb);
      dcn += sz;
      b.pop_back();
    }
    if (ina && inb) {
      cn = min(cn, dcn);
    }
    ans += cn;
    both.pop_back();
  }
  cout << ans << '\n';
}
