#include <bits/stdc++.h>
using namespace std;
queue<int> is, b;
char geto[5];
int m[300005];
bool mb[300005], nb = true, nr = true, fb, fr;
int main() {
  int i = 0, n, pre, suc, sub, sur, prb, prr;
  double cost = 0, mxb, mxr;
  scanf("%d", &n);
  for (int j = 0; j < n; j++) {
    scanf("%d%s", &m[j], geto);
    if (geto[0] == 'G') is.push(j);
    if (geto[0] == 'B') mb[j] = true;
  }
  if (is.empty()) {
    prb = prr = 0;
    sub = sur = n - 1;
    while (prb < n) {
      if (mb[prb]) break;
      prb++;
    }
    while (prr < n) {
      if (!mb[prr]) break;
      prr++;
    }
    if (prb != n - 1)
      while (sub >= 0) {
        if (mb[sub]) break;
        sub--;
      }
    if (prr != n - 1)
      while (sur >= 0) {
        if (!mb[sur]) break;
        sur--;
      }
    cost += m[sub] - m[prb] + m[sur] - m[prr];
  } else {
    suc = is.front();
    is.pop();
    while (i < suc) {
      if (mb[i] && nb) cost += m[suc] - m[i], nb = false;
      if (!mb[i] && nr) cost += m[suc] - m[i], nr = false;
      i++;
    }
    for (i++; i < n; i++) {
      nb = nr = false;
      fb = fr = true;
      mxb = mxr = -1;
      pre = suc;
      if (!is.empty()) {
        suc = is.front();
        is.pop();
      }
      if (pre == suc) {
        i = n - 1;
        while (i > pre) {
          if (mb[i] && !nb) cost += m[i] - m[pre], nb = true;
          if (!mb[i] && !nr) cost += m[i] - m[pre], nr = true;
          i--;
        }
        break;
      } else {
        sub = prb = sur = prr = pre;
        while (i < suc) {
          if (mb[i]) {
            sub = i;
            nb = true;
            if (m[sub] - m[prb] > mxb) mxb = m[sub] - m[prb];
            prb = sub;
          } else {
            sur = i;
            nr = true;
            if (m[sur] - m[prr] > mxr) mxr = m[sur] - m[prr];
            prr = sur;
          }
          i++;
        }
        if (nb || nr) {
          if (sub != pre) mxb = max(mxb, (double)m[suc] - m[sub]);
          if (sur != pre) mxr = max(mxr, (double)m[suc] - m[sur]);
          if (mxr == -1)
            cost += ((m[suc] - m[pre]) << 1) - mxb;
          else if (mxb == -1)
            cost += ((m[suc] - m[pre]) << 1) - mxr;
          else
            cost += ((m[suc] - m[pre]) << 1) +
                    min(0.0, m[suc] - m[pre] - mxb - mxr);
        } else
          cost += m[suc] - m[pre];
      }
    }
  }
  printf("%.0lf", cost);
}
