#include <bits/stdc++.h>
using namespace std;
pair<int, char> arr[200000];
int n;
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d %c", &arr[i].first, &arr[i].second);
  long long ans = 0, ansB = 0, ansR = 0;
  int firstP = -1, firstB = -1, firstR = -1;
  for (int i = 0; i < n; i++)
    if (arr[i].second == 'P') {
      if (firstP == -1) firstP = i;
      ans = arr[i].first - arr[firstP].first;
    } else if (arr[i].second == 'B') {
      if (firstB == -1) firstB = i;
      ansB = arr[i].first - arr[firstB].first;
    } else {
      if (firstR == -1) firstR = i;
      ansR = arr[i].first - arr[firstR].first;
    }
  if (firstP == -1) {
    printf("%lld\n", ansB + ansR);
    return 0;
  }
  int lastP = -1, lastB = -1, lastR = -1;
  int distB = 0, distR = 0;
  for (int i = 0; i < n; i++) {
    if (arr[i].second == 'P') {
      if (lastP == -1) {
        if (lastB != -1) ans += arr[i].first - arr[lastB].first;
        if (lastR != -1) ans += arr[i].first - arr[lastR].first;
      } else {
        if (lastB != -1 && lastR != -1) {
          int val = arr[i].first - arr[lastP].first -
                    max(distB, arr[i].first - arr[lastB].first) + arr[i].first -
                    arr[lastP].first -
                    max(distR, arr[i].first - arr[lastR].first);
          ans += min((arr[i].first - arr[lastP].first), val);
        } else {
          if (lastB != -1)
            ans += arr[i].first - arr[lastP].first -
                   max(distB, arr[i].first - arr[lastB].first);
          if (lastR != -1)
            ans += arr[i].first - arr[lastP].first -
                   max(distR, arr[i].first - arr[lastR].first);
        }
      }
      lastB = lastR = -1;
      lastP = i;
      distR = distB = 0;
    } else if (arr[i].second == 'B') {
      if (lastP != -1) {
        if (lastB != -1)
          distB = max(distB, arr[i].first - arr[lastB].first);
        else
          distB = max(distB, arr[i].first - arr[lastP].first);
      } else {
        if (lastB != -1) ans += arr[i].first - arr[lastB].first;
      }
      lastB = i;
    } else {
      if (lastP != -1) {
        if (lastR != -1)
          distR = max(distR, arr[i].first - arr[lastR].first);
        else
          distR = max(distR, arr[i].first - arr[lastP].first);
      } else {
        if (lastR != -1) ans += arr[i].first - arr[lastR].first;
      }
      lastR = i;
    }
  }
  reverse(arr, arr + n);
  lastB = lastP = lastR = -1;
  for (int i = 0; i < n; i++) {
    if (arr[i].second == 'P') {
      if (lastB != -1) ans += abs(arr[i].first - arr[lastB].first);
      if (lastR != -1) ans += abs(arr[i].first - arr[lastR].first);
      break;
    } else if (arr[i].second == 'B') {
      if (lastP == -1)
        if (lastB != -1) ans += -arr[i].first + arr[lastB].first;
      lastB = i;
    } else {
      if (lastP == -1)
        if (lastR != -1) ans += -arr[i].first + arr[lastR].first;
      lastR = i;
    }
  }
  printf("%lld\n", ans);
  return 0;
}
