#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  long long n;
  cin >> n;
  long long ans = 0;
  deque<long long> X[256];
  while (n--) {
    long long x;
    char c;
    cin >> x >> c;
    X[c].push_back(x);
  }
  if (X['G'].empty()) {
    for (long long i = 1; i < X['R'].size(); i++)
      ans += X['R'][i] - X['R'][i - 1];
    for (long long i = 1; i < X['B'].size(); i++)
      ans += X['B'][i] - X['B'][i - 1];
    cout << ans;
    return 0;
  }
  {
    vector<long long> A;
    vector<long long> B;
    int a = X['G'].front();
    while (!X['R'].empty() && X['R'].front() < a) {
      A.push_back(X['R'].front());
      X['R'].pop_front();
    }
    while (!X['B'].empty() && X['B'].front() < a) {
      B.push_back(X['B'].front());
      X['B'].pop_front();
    }
    A.push_back(a);
    B.push_back(a);
    for (long long i = 1; i < A.size(); i++) ans += A[i] - A[i - 1];
    for (long long i = 1; i < B.size(); i++) ans += B[i] - B[i - 1];
  }
  {
    vector<long long> A;
    vector<long long> B;
    while (!X['R'].empty() && X['R'].back() > X['G'].back()) {
      A.push_back(X['R'].back());
      X['R'].pop_back();
    }
    while (!X['B'].empty() && X['B'].back() > X['G'].back()) {
      B.push_back(X['B'].back());
      X['B'].pop_back();
    }
    A.push_back(X['G'].back());
    B.push_back(X['G'].back());
    for (long long i = 1; i < A.size(); i++) ans += A[i - 1] - A[i];
    for (long long i = 1; i < B.size(); i++) ans += B[i - 1] - B[i];
  }
  for (long long i = 1; i < X['G'].size(); i++) {
    long long a = X['G'][i - 1];
    long long b = X['G'][i];
    vector<long long> A;
    vector<long long> B;
    A.push_back(a);
    B.push_back(a);
    while (!X['R'].empty() && X['R'].front() < b) {
      A.push_back(X['R'].front());
      X['R'].pop_front();
    }
    while (!X['B'].empty() && X['B'].front() < b) {
      B.push_back(X['B'].front());
      X['B'].pop_front();
    }
    A.push_back(b);
    B.push_back(b);
    if (A.size() + B.size() == 4) {
      ans += b - a;
      continue;
    }
    long long ret = 2 * (b - a);
    long long ret1 = ret + b - a;
    long long a1 = 0;
    long long a2 = 0;
    for (long long i = 1; i < A.size(); i++) a1 = max(a1, A[i] - A[i - 1]);
    for (long long i = 1; i < B.size(); i++) a2 = max(a2, B[i] - B[i - 1]);
    ret1 -= a1 + a2;
    ans += min(ret1, ret);
  }
  cout << ans;
}
