#include <bits/stdc++.h>
using namespace std;
mt19937 Random((long long)time(0));
const long long INF = 1e9 + 10;
const long long MOD = 1e9 + 7;
const long long P = 239017;
const long long day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const long long BINF = 1e18 + 10;
const long long N = 3e5 + 10;
deque<long long> arr_r, arr_b;
vector<long long> arr_g, before_r[N], before_b[N];
inline long long get_sum(deque<long long> arr) {
  return (arr.empty() ? 0 : arr.back() - arr.front());
}
inline long long get_max(vector<long long> arr) {
  long long res = 0;
  for (long long i = 1; i < arr.size(); i++) {
    res = max(res, arr[i] - arr[i - 1]);
  }
  return res;
}
inline void source() {
  long long n, ans = 0;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    long long pos;
    char ch;
    cin >> pos >> ch;
    if (ch == 'R') {
      arr_r.push_back(pos);
    } else if (ch == 'G') {
      arr_g.push_back(pos);
    } else {
      arr_b.push_back(pos);
    }
  }
  if (arr_g.empty()) {
    if (!arr_r.empty()) {
      ans += get_sum(arr_r);
    }
    if (!arr_b.empty()) {
      ans += get_sum(arr_b);
    }
    cout << ans;
    exit(0);
  }
  if (!arr_r.empty() && arr_r.front() <= arr_g.front()) {
    ans += arr_g.front() - arr_r.front();
    while (!arr_r.empty() && arr_r.front() <= arr_g.front()) {
      arr_r.pop_front();
    }
  }
  if (!arr_b.empty() && arr_b.front() <= arr_g.front()) {
    ans += arr_g.front() - arr_b.front();
    while (!arr_b.empty() && arr_b.front() <= arr_g.front()) {
      arr_b.pop_front();
    }
  }
  if (!arr_r.empty() && arr_r.back() >= arr_g.back()) {
    ans += arr_r.back() - arr_g.back();
    while (!arr_r.empty() && arr_r.back() >= arr_g.back()) {
      arr_r.pop_back();
    }
  }
  if (!arr_b.empty() && arr_b.back() >= arr_g.back()) {
    ans += arr_b.back() - arr_g.back();
    while (!arr_b.empty() && arr_b.back() >= arr_g.back()) {
      arr_b.pop_back();
    }
  }
  for (long long i = 1; i < arr_g.size(); i++) {
    while (!arr_r.empty() && arr_r.front() <= arr_g[i]) {
      before_r[i].push_back(arr_r.front());
      arr_r.pop_front();
    }
    while (!arr_b.empty() && arr_b.front() <= arr_g[i]) {
      before_b[i].push_back(arr_b.front());
      arr_b.pop_front();
    }
    if ((!before_b[i].empty() && before_r[i].empty()) ||
        (before_b[i].empty() && !before_r[i].empty())) {
      ans += arr_g[i] - arr_g[i - 1];
      if (!before_b[i].empty()) {
        long long mx =
            max(get_max(before_b[i]), max(before_b[i].front() - arr_g[i - 1],
                                          arr_g[i] - before_b[i].back()));
        ans += arr_g[i] - arr_g[i - 1] - mx;
      }
      if (!before_r[i].empty()) {
        long long mx =
            max(get_max(before_r[i]), max(before_r[i].front() - arr_g[i - 1],
                                          arr_g[i] - before_r[i].back()));
        ans += arr_g[i] - arr_g[i - 1] - mx;
      }
    } else {
      ans += arr_g[i] - arr_g[i - 1];
      long long sum = 0;
      if (!before_b[i].empty()) {
        long long mx =
            max(get_max(before_b[i]), max(before_b[i].front() - arr_g[i - 1],
                                          arr_g[i] - before_b[i].back()));
        sum += arr_g[i] - arr_g[i - 1] - mx;
      }
      if (!before_r[i].empty()) {
        long long mx =
            max(get_max(before_r[i]), max(before_r[i].front() - arr_g[i - 1],
                                          arr_g[i] - before_r[i].back()));
        sum += arr_g[i] - arr_g[i - 1] - mx;
      }
      ans += min(arr_g[i] - arr_g[i - 1], sum);
    }
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  srand(time(0));
  source();
  return 0;
}
