#include <bits/stdc++.h>
using namespace std;
int n, lst[3], mx[2];
long long ans, pos[300005];
char color;
inline char get() {
  char in = getchar();
  while (in != 'R' && in != 'G' && in != 'B') {
    in = getchar();
  }
  return in;
}
inline void did(int opt, int nowpos) {
  if (lst[opt] != 0) {
    ans += pos[nowpos] - pos[lst[opt]];
    mx[opt] =
        (mx[opt] > pos[nowpos] - pos[lst[opt]] ? mx[opt]
                                               : pos[nowpos] - pos[lst[opt]]);
  }
  lst[opt] = nowpos;
}
inline void did2(int nowpos) {
  if (lst[2] != 0) {
    ans += (0 < pos[nowpos] - pos[lst[2]] - mx[0] - mx[1]
                ? 0
                : pos[nowpos] - pos[lst[2]] - mx[0] - mx[1]);
  }
  lst[2] = nowpos;
  mx[0] = 0;
  mx[1] = 0;
}
int main() {
  scanf("%d", &n);
  for (register int i = 1; i <= n; i++) {
    scanf("%d", &pos[i]);
    color = get();
    switch (color) {
      case 'R':
        did(0, i);
        break;
      case 'B':
        did(1, i);
        break;
      case 'G':
        did(0, i);
        did(1, i);
        did2(i);
        break;
    }
  }
  printf("%lld\n", ans);
  return 0;
}
