#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 123;
vector<int> a[4];
int typ[N], val[N];
int mx[4];
void load() {}
void process() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    char c;
    cin >> val[i] >> c;
    int p = 0;
    if (c == 'R') p = 1;
    if (c == 'B') p = 2;
    typ[i] = p;
    a[p].push_back(i);
  }
  if (a[0].size() == 0) {
    long long ans = 0;
    if (a[1].size()) ans += ((long long)(val[a[1].back()] - val[a[1][0]]));
    if (a[2].size()) ans += ((long long)(val[a[2].back()] - val[a[2][0]]));
    printf("%lld", ans);
    return;
  }
  int x = val[a[0][0]];
  long long ans = 0;
  if (a[1].size() && val[a[1][0]] < x) ans += ((long long)(x - val[a[1][0]]));
  if (a[2].size() && val[a[2][0]] < x) ans += ((long long)(x - val[a[2][0]]));
  x = val[a[0].back()];
  if (a[1].size() && val[a[1].back()] > x)
    ans += ((long long)(val[a[1].back()] - x));
  if (a[2].size() && val[a[2].back()] > x)
    ans += ((long long)(val[a[2].back()] - x));
  for (int i = 0; i + 1 < a[0].size(); ++i) {
    int s = a[0][i];
    int t = a[0][i + 1];
    vector<int> mem[3];
    mem[1].push_back(val[s]);
    mem[2].push_back(val[s]);
    for (int j = s + 1; j < t; ++j) mem[typ[j]].push_back(val[j]);
    mem[1].push_back(val[t]);
    mem[2].push_back(val[t]);
    mx[1] = 0, mx[2] = 0;
    for (int j = 1; j < 3; ++j)
      for (int k = 0; k + 1 < mem[j].size(); ++k)
        mx[j] = max(mx[j], mem[j][k + 1] - mem[j][k]);
    ans +=
        ((long long)min((long long)2LL * (val[t] - val[s]),
                        (long long)(3LL * (val[t] - val[s]) - mx[1] - mx[2])));
  }
  printf("%lld", ans);
  return;
}
int main() {
  load();
  process();
  return 0;
}
