#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U>
ostream& operator<<(ostream& o, const pair<T, U>& p) {
  o << "(" << p.first << "," << p.second << ")";
  return o;
}
template <class T>
ostream& operator<<(ostream& o, const vector<T>& v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}
const ll INF = LLONG_MAX / 3;
int main() {
  int n;
  cin >> n;
  vector<ll> a[2], p;
  for (int i = 0; i < (int)(n); ++i) {
    int x;
    char c;
    cin >> x >> c;
    if (c == 'R')
      a[0].push_back(x);
    else if (c == 'B')
      a[1].push_back(x);
    else
      p.push_back(x);
  }
  if (p.empty()) {
    ll ans = 0;
    for (auto& v : {a[0], a[1]}) {
      if (v.empty()) continue;
      ll mn = INF, mx = -INF;
      for (ll i : v) {
        mn = min(mn, i);
        mx = max(mx, i);
      }
      ans += mx - mn;
    }
    cout << ans << "\n";
    return 0;
  }
  ll ans = 0;
  int sz[2];
  for (int i = 0; i < (int)(2); ++i) sz[i] = a[i].size();
  int P = p.size();
  int idx[2] = {};
  for (int i = 0; i < (int)(2); ++i) {
    if (!a[i].empty() && a[i][0] < p[0]) {
      ans += p[0] - a[i][0];
      while (idx[i] < sz[i] && a[i][idx[i]] < p[0]) ++idx[i];
    }
  }
  for (int pidx = 0; pidx < (int)(P - 1); ++pidx) {
    ll L = p[pidx], R = p[pidx + 1];
    ll W = R - L;
    ll g[2] = {};
    for (int i = 0; i < (int)(2); ++i) {
      vector<ll> v;
      v.push_back(L);
      while (idx[i] < sz[i] && a[i][idx[i]] < R) v.push_back(a[i][idx[i]++]);
      v.push_back(R);
      int V = v.size();
      for (int j = 0; j < (int)(V - 1); ++j) g[i] = max(g[i], v[j + 1] - v[j]);
    }
    ans += min(2 * W, 3 * W - g[0] - g[1]);
  }
  for (int i = 0; i < (int)(2); ++i) {
    if (!a[i].empty() && p[P - 1] < a[i][sz[i] - 1])
      ans += a[i][sz[i] - 1] - p[P - 1];
  }
  cout << ans << "\n";
  return 0;
}
