#include <bits/stdc++.h>
using namespace std;
template <typename T>
void _ma(T &a, T b) {
  if (a < b) a = b;
}
template <typename T>
void _mi(T &a, T b) {
  if (a > b) a = b;
}
long long n, p, ans, first, second, halp, ugu;
char c;
vector<pair<long long, bool> > b, r;
int vis[4];
int main() {
  scanf("%I64d", &n);
  int i;
  for (i = 0; i < n; i++) {
    scanf("%I64d", &p);
    scanf(" %c", &c);
    if (c == 'B') {
      b.push_back({p, 0});
      vis[1] = 1;
    } else if (c == 'R') {
      r.push_back({p, 0});
      vis[2] = 1;
    } else {
      b.push_back({p, 1});
      r.push_back({p, 1});
      vis[3] = 1;
    }
  }
  if (vis[1] + vis[2] + vis[3] == 1) {
    if (b.size()) {
      for (i = 1; i < b.size(); i++) ans += b[i].first - b[i - 1].first;
    } else
      for (i = 1; i < r.size(); i++) ans += r[i].first - r[i - 1].first;
  } else if (vis[1] + vis[2] + vis[3] == 2 && !vis[3]) {
    for (i = 1; i < b.size(); i++) ans += b[i].first - b[i - 1].first;
    for (i = 1; i < r.size(); i++) ans += r[i].first - r[i - 1].first;
  } else {
    p = 1;
    if (vis[1] && !b[0].second)
      while (!b[p - 1].second) ans += b[p].first - b[p - 1].first, p++;
    b.erase(b.begin(), b.begin() + p - 1);
    p = 1;
    if (vis[2] && !r[0].second)
      while (!r[p - 1].second) ans += r[p].first - r[p - 1].first, p++;
    r.erase(r.begin(), r.begin() + p - 1);
    p = b.size() - 2;
    if (vis[1] && !b[b.size() - 1].second)
      while (!b[p + 1].second) ans += b[p + 1].first - b[p].first, p--;
    b.erase(b.begin() + p + 2, b.end());
    p = r.size() - 2;
    if (vis[2] && !r[r.size() - 1].second)
      while (!r[p + 1].second) ans += r[p + 1].first - r[p].first, p--;
    r.erase(r.begin() + p + 2, r.end());
    if (!vis[1]) {
      for (i = 0; i < r.size() - 1;) {
        p = i + 1, halp = r[p].first - r[p - 1].first;
        while (!r[p].second) p++, halp = max(halp, r[p].first - r[p - 1].first);
        ans += ((r[p].first - r[i].first) * 2) - halp;
        i = p;
      }
    } else if (!vis[2]) {
      for (i = 0; i < b.size() - 1;) {
        p = i + 1, halp = b[p].first - b[p - 1].first;
        while (!b[p].second) p++, halp = max(halp, b[p].first - b[p - 1].first);
        ans += ((b[p].first - b[i].first) * 2) - halp;
        i = p;
      }
    } else {
      first = second = 0;
      while (first != b.size() - 1 && second != r.size() - 1) {
        p = first + 1, n = second + 1, halp = b[p].first - b[p - 1].first,
        ugu = r[n].first - r[n - 1].first;
        while (!b[p].second) p++, halp = max(halp, b[p].first - b[p - 1].first);
        while (!r[n].second) n++, ugu = max(ugu, r[n].first - r[n - 1].first);
        ans += min(((r[n].first - r[second].first) * 2ll),
                   ((r[n].first - r[second].first) * 3) - halp - ugu);
        second = n, first = p;
      }
    }
  }
  printf("%I64d\n", ans);
}
