#include <bits/stdc++.h>
using namespace std;
vector<long long> o[3], p[3];
long long n, t[300010], dis[300010], lst = 0;
char s[3];
long long res;
signed main() {
  scanf("%lld", &n);
  for (long long i = 1; i <= n; i++) {
    scanf("%lld", &dis[i]);
    scanf("%s", s + 1);
    if (s[1] == 'B') t[i] = 1;
    if (s[1] == 'P') t[i] = 2;
    o[t[i]].push_back(dis[i]);
  }
  if (!o[2].size()) {
    printf("%lld\n", (o[1].size() ? o[1].back() - o[1][0] : 0) +
                         (o[0].size() ? o[0].back() - o[0][0] : 0));
    return 0;
  }
  for (long long i = 1; i <= n; i++)
    if (t[i] == 2) {
      for (long long j = lst + 1; j < i; j++) p[t[j]].push_back(dis[j]);
      if (!lst) {
        for (long long tp = 1; ~tp; tp--)
          if (p[tp].size()) res += dis[i] - p[tp][0];
        lst = i;
      } else {
        long long ret = 0;
        for (long long tp = 1, d; ~tp; tp--) {
          d = 0;
          for (long long j = 1; j < p[tp].size(); j++)
            d = max(d, p[tp][j] - p[tp][j - 1]);
          if (p[tp].size())
            ret += dis[i] - dis[lst] -
                   max(d, max(p[tp][0] - dis[lst], dis[i] - p[tp].back()));
        }
        res += min(ret, dis[i] - dis[lst]) + dis[i] - dis[lst];
        lst = i;
      }
      for (long long j = 1; ~j; j--) p[j].clear();
    }
  for (long long i = lst + 1; i <= n; i++) p[t[i]].push_back(dis[i]);
  for (long long tp = 1; ~tp; tp--)
    if (p[tp].size()) res += p[tp].back() - dis[lst];
  printf("%lld\n", res);
}
