#include <bits/stdc++.h>
using namespace std;
long long poww(long long a, long long b, long long md) {
  return (!b ? 1
             : (b & 1 ? a * poww(a * a % md, b / 2, md) % md
                      : poww(a * a % md, b / 2, md) % md));
}
const long long MAXN = 1e6 + 10;
const long long INF = 8e18;
const long long MOD = 1e9 + 7;
vector<pair<long long, long long> > P;
vector<int> GR, GB;
long long ans = 0, n;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x;
    char c;
    cin >> x >> c;
    if (c == 'G') {
      GR.push_back(x);
      GB.push_back(x);
      P.push_back({x, 0});
    } else if (c == 'R') {
      GR.push_back(x);
      P.push_back({x, 1});
    } else {
      GB.push_back(x);
      P.push_back({x, 2});
    }
  }
  sort((GB).begin(), (GB).end());
  sort((GR).begin(), (GR).end());
  sort((P).begin(), (P).end());
  if (!GB.empty()) ans += GB.back() - GB.front();
  if (!GR.empty()) ans += GR.back() - GR.front();
  int last_g = 0, b_r = 0, b_b = 0, last_gr = 0, last_gb = 0;
  for (int i = 0; i < n; i++) {
    int c = P[i].second, x = P[i].first;
    if ((c == 0 || c == 1)) {
      if (last_gr > 0) b_r = max(b_r, x - last_gr);
      last_gr = x;
    }
    if ((c == 0 || c == 2)) {
      if (last_gb > 0) b_b = max(b_b, x - last_gb);
      last_gb = x;
    }
    if (c == 0) {
      if (last_g)
        if (x - last_g < b_b + b_r) ans += x - last_g - b_b - b_r;
      b_r = 0;
      b_b = 0;
      last_g = x;
    }
  }
  cout << ans << '\n';
  return 0;
}
