#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int inf = 2e9;
LL calc(set<int>& st, int L, int R) {
  int ans = 0;
  for (int i = L, j = L;;) {
    ans = max(ans, i - j);
    if (i == R) break;
    j = i;
    i = st.upper_bound(i) != st.end() && *st.upper_bound(i) < R
            ? i = *st.upper_bound(i)
            : R;
  }
  return ans;
}
int main() {
  int n;
  cin >> n;
  set<int> a, b, c;
  for (int i = 0; i < n; i++) {
    int x;
    char ch;
    scanf("%d %c", &x, &ch);
    if (ch == 'B') a.insert(x);
    if (ch == 'R') b.insert(x);
    if (ch == 'P') c.insert(x);
  }
  int B = 0, R = 0;
  int mn = inf, mx = -inf;
  if (a.size()) mn = min(mn, *a.begin()), mx = max(mx, *--a.end());
  if (c.size()) mn = min(mn, *c.begin()), mx = max(mx, *--c.end());
  if (mn != inf) B = mx - mn;
  mn = inf, mx = -inf;
  if (b.size()) mn = min(mn, *b.begin()), mx = max(mx, *--b.end());
  if (c.size()) mn = min(mn, *c.begin()), mx = max(mx, *--c.end());
  if (mn != inf) R = mx - mn;
  LL ans = (LL)B + R;
  if (!B || !R || c.empty()) return cout << ans, 0;
  for (auto i = c.begin(); ++i != c.end(); i++) {
    auto j = i--;
    ans += min(0ll, *j - *i - calc(a, *i, *j) - calc(b, *i, *j));
  }
  cout << ans << endl;
  return 0;
}
