#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 2;
long long n, x[N], mx[4], mn[4], ans;
char c[N];
vector<long long> v1, v2, v3, v, t;
int main() {
  mx[1] = -2e9;
  mx[2] = -2e9;
  mx[3] = -2e9;
  mn[1] = 2e9;
  mn[2] = 2e9;
  mn[3] = 2e9;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x[i] >> c[i];
    if (c[i] == 'B') {
      mx[1] = max(mx[1], x[i]);
      mn[1] = min(mn[1], x[i]);
      v1.push_back(x[i]);
    }
    if (c[i] == 'R') {
      mx[2] = max(mx[2], x[i]);
      mn[2] = min(mn[2], x[i]);
      v2.push_back(x[i]);
    }
    if (c[i] == 'P') {
      mx[3] = max(mx[3], x[i]);
      mn[3] = min(mn[3], x[i]);
      v3.push_back(x[i]);
    }
  }
  if (mx[1] == -2e9 && mx[2] == -2e9 && mx[3] != -2e9) {
    ans = mx[3] - mn[3];
  } else if (mx[1] == -2e9 && mx[2] != -2e9 && mx[3] == -2e9) {
    ans = mx[2] - mn[2];
  } else if (mx[1] != -2e9 && mx[2] == -2e9 && mx[3] == -2e9) {
    ans = mx[1] - mn[1];
  } else if (mx[1] != -2e9 && mx[2] != -2e9 && mx[3] == -2e9) {
    ans = mx[1] - mn[1];
    ans += mx[2] - mn[2];
  } else {
    if (v3.size() == 1) {
      ans = max(mx[1], mx[3]) - min(mn[1], mn[3]);
      ans += max(mx[2], mx[3]) - min(mn[2], mn[3]);
    } else {
      int p1 = 0, p2 = 0;
      for (int i = int(v2.size()) - 1; i >= 0; i--) {
        if (v2[i] < v3.front()) {
          ans = v3.front() - v2.front();
          break;
        }
      }
      for (int i = 0; i < v2.size(); i++) {
        if (v2[i] > v3.back()) {
          ans += v2.back() - v3.back();
          break;
        }
      }
      for (int i = int(v1.size()) - 1; i >= 0; i--) {
        if (v1[i] < v3.front()) {
          ans += v3.front() - v1.front();
          break;
        }
      }
      for (int i = 0; i < v1.size(); i++) {
        if (v1[i] > v3.back()) {
          ans += v1.back() - v3.back();
          break;
        }
      }
      for (int i = 1; i < v3.size(); i++) {
        long long res = (v3[i] - v3[i - 1]) * 2;
        v.clear();
        t.clear();
        while (p1 < v1.size()) {
          if (v1[p1] > v3[i]) break;
          if (v1[p1] > v3[i - 1]) v.push_back(v1[p1]);
          p1++;
        }
        while (p2 < v2.size()) {
          if (v2[p2] > v3[i]) break;
          if (v2[p2] > v3[i - 1]) t.push_back(v2[p2]);
          p2++;
        }
        long long b = v3[i] - v3[i - 1];
        if (v.size()) {
          long long c = 0;
          c = min(v3[i] - v.front(), v.back() - v3[i - 1]);
          for (int j = 1; j < v.size(); j++) {
            c = min(c, v[j - 1] - v3[i - 1] + (v3[i] - v[j]));
          }
          b += c;
        }
        if (t.size()) {
          long long c = 0;
          c = min(v3[i] - t.front(), t.back() - v3[i - 1]);
          for (int j = 1; j < t.size(); j++) {
            c = min(c, t[j - 1] - v3[i - 1] + (v3[i] - t[j]));
          }
          b += c;
        }
        res = min(res, b);
        ans += res;
      }
    }
  }
  cout << ans;
}
