#include <bits/stdc++.h>
using namespace std;
int N;
long long P[300005];
char C[300005];
vector<long long> red, blue;
int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%I64d %c", &P[i], &C[i]);
  }
  int g = -1, r = -1, b = -1;
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    if (C[i] == 'R') {
      int u = max(g, r);
      if (u != -1) red.push_back(abs(P[i] - P[u]));
      r = i;
    } else if (C[i] == 'B') {
      int u = max(g, b);
      if (u != -1) blue.push_back(abs(P[i] - P[u]));
      b = i;
    } else {
      if (r != -1) red.push_back(abs(P[i] - P[r]));
      if (b != -1) blue.push_back(abs(P[i] - P[b]));
      sort(red.begin(), red.end(), greater<long long>());
      sort(blue.begin(), blue.end(), greater<long long>());
      if (g == -1) {
        for (int j = 0; j < red.size(); j++) ans += red[j];
        for (int j = 0; j < blue.size(); j++) ans += blue[j];
      } else {
        long long d = abs(P[i] - P[g]), add = d;
        if (red.size() > 0) add += (d - red[0]);
        if (blue.size() > 0) add += (d - blue[0]);
        ans += min(2 * d, add);
      }
      red.clear();
      blue.clear();
      g = i;
      r = -1, b = -1;
    }
  }
  for (int j = 0; j < red.size(); j++) ans += red[j];
  for (int j = 0; j < blue.size(); j++) ans += blue[j];
  printf("%I64d\n", ans);
}
