#include <bits/stdc++.h>   using namespace std;   long long n, m, a, b, c, d, e, R, B, P, ans;   char s;   inline long long read() {     long long sum = 0, x = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) x = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       sum = sum * 10 + ch -  0 ;       ch = getchar();     }     return sum * x;   }   inline void write(long long x) {     if (x < 0) {       putchar( - );       x = -x;     }     if (x > 9) write(x / 10);     putchar(x % 10 +  0 );     return;   }   signed main() {     n = read();     for (register long long i = 1; i <= n; ++i) {       m = read();       cin >> s;       if (s ==  P  || s ==  R ) {         if (R) {           ans += m - b;           a = max(a, m - b);         }         R = 1;         b = m;       }       if (s ==  P  || s ==  B ) {         if (B) {           ans += m - c;           d = max(d, m - c);         }         B = 1;         c = m;       }       if (s ==  P ) {         if (P) ans += min(0ll, m - e - d - a);         P = 1;         a = d = 0;         e = m;       }     }     write(ans);     return 0;   }   