#include <bits/stdc++.h>   using namespace std;   const int M = 100001;   int n, m, x, y, b, r, p, l, ri;   long long ans;   vector<int> B, R, P;   int bef(int x, int c) {     if (!c) return (lower_bound(B.begin(), B.end(), x) - B.begin() - 1);     return (lower_bound(R.begin(), R.end(), x) - R.begin() - 1);   }   int aft(int x, int c) {     if (!c) return (lower_bound(B.begin(), B.end(), x) - B.begin());     return (lower_bound(R.begin(), R.end(), x) - R.begin());   }   long long get(int x) {     long long mn = ri - B[x];     for (int i = x; i < b && B[i] < ri; i++) {       long long left = B[i] - l, right = 0;       if (i + 1 != b && B[i + 1] < ri) right = ri - B[i + 1];       mn = min(mn, left + right);     }     return mn;   }   long long get1(int x) {     long long mn = ri - R[x];     for (int i = x; i < r && R[i] < ri; i++) {       long long left = R[i] - l, right = 0;       if (i + 1 != r && R[i + 1] < ri) right = ri - R[i + 1];       mn = min(mn, left + right);     }     return mn;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cin >> n;     for (int i = 0; i < n; i++) {       char c;       cin >> x >> c;       if (c ==  B )         B.push_back(x);       else if (c ==  R )         R.push_back(x);       else         P.push_back(x);     }     b = B.size();     r = R.size();     p = P.size();     if (!P.size()) {       if (b) ans = B[b - 1] - B[0];       if (r) ans += R[r - 1] - R[0];       cout << ans;       return 0;     }     x = bef(P[0], 0);     y = bef(P[0], 1);     if (x >= 0) ans += P[0] - B[0];     if (y >= 0) ans += P[0] - R[0];     x = aft(P[p - 1], 0);     y = aft(P[p - 1], 1);     if (x != B.size()) ans += B[b - 1] - P[p - 1];     if (y != R.size()) ans += R[r - 1] - P[p - 1];     for (int i = 0; i < p - 1; i++) {       l = P[i];       ri = P[i + 1];       long long dist = ri - l;       int x = aft(l, 0), y = aft(l, 1);       if ((x == b || B[x] > ri) && (y == r || R[y] > ri)) {         ans += dist;         continue;       }       long long mn = 2 * dist;       long long k = dist;       if (x < b && B[x] < ri) k += get(x);       if (y < r && R[y] < ri) k += get1(y);       ans += min(mn, k);     }     cout << ans;     return 0;   }   