#include <bits/stdc++.h>
using namespace std;

int main() {
    int N;
    cin >> N;
    N *= 2;
    vector<pair<int64_t, char>> P(N);
    bool ro = false, go = false, bo = false;
    for (int i = 0; i < N; i++) {
        cin >> P[i].first >> P[i].second;
        if (P[i].second == 'R') ro ^= true;
        if (P[i].second == 'G') go ^= true;
        if (P[i].second == 'B') bo ^= true;
    }
    sort(P.begin(), P.end());

    int64_t R, G, B, RG, RB, GB;
    R = G = B = -1e16;
    RG = RB = GB = 1e16;
    for (int i = 0; i < N; i++) {
        if (P[i].second == 'R') {
            R = P[i].first;
            RG = min(RG, R - G);
            RB = min(RB, R - B);
        } else if (P[i].second == 'G') {
            G = P[i].first;
            RG = min(RG, G - R);
            GB = min(GB, G - B);
        } else if (P[i].second == 'B') {
            B = P[i].first;
            GB = min(GB, B - G);
            RB = min(RB, B - R);
        }
    }
    if (!ro && !go && !bo) cout << 0 << endl;
    else if (ro && go && !bo) cout << min(RG, RB + GB) << endl;
    else if (ro && !go && bo) cout << min(RB, RG + GB) << endl;
    else if (!ro && go && bo) cout << min(GB, RG + RB) << endl;
}
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h> 

using namespace std;
using i128 = __int128_t;
using u128 = __uint128_t;
using u64 = uint64_t;

//define
#define int long long
#define ll int
#define trav(i,v)   for(auto i: v)
#define rep(i,n)    for(int i=0;i<n;i++)
#define repu(i,k,n) for(int i=k;i<=n;i++)
#define repd(i,k,n) for(int i=k;i>=n;i--)
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define all(v)  v.begin(), v.end()
#define itn int 
#define sz(x) (int)x.size()
    
//typedef
typedef unsigned long long ull;
typedef long double ld;
typedef std::pair<int, int> pii;
typedef std::vector<int> vi;
typedef std::vector< pii > vii;
typedef std::vector< std::vector<int> > vvi;
typedef std::vector< std::pair < pii, int > > vpp;
 
const long long MOD = 1000000007;
// const long long MOD = 998244353;
const long double PI = 3.141592653589793238;
const long long pi = 31415926;
const long long inf = 1000000000000000000;
const long long small_inf = INT_MAX;

int modpow(int x, int n, int mod = MOD){ int res=1; while(n>0){ if(n&1) res=res*x%mod; x=x*x%MOD; n>>=1;} return res;}
int power(int x, int n){ int res=1; while(n>0){ if(n&1) res=res*x; x=x*x; n>>=1; } return res;}

void init(){
}

int find(int a, int b, vi *v){
    int n1 = sz(v[a]), n2 = sz(v[b]);
    int x = 0;
    int mn = inf;
    repu(y,0,n2-1){
        while(x<n1-1 && v[a][x]<v[b][y])
            x++;
        mn = min(mn,abs(v[a][x]-v[b][y]));
        if(x)
            mn = min(mn,abs(v[a][x-1]-v[b][y]));
    }
    return mn;
}

void solve()
{
/*Don't hurry, nothing good comes if you rush*/
    int n;
    cin>>n;
    vi cnt(3,0);
    map<char, int> mm;
    mm['R'] = 0;
    mm['G'] = 1;
    mm['B'] = 2;
    vector<char> c(2*n+1,0);
    vi v[3];
    repu(i,1,2*n){
        int tmp;
        cin>>tmp>>c[i];
        cnt[mm[c[i]]]++;
        v[mm[c[i]]].pb(tmp);
    }
    int odd = 0, f=-1, a=-1, b=-1;
    rep(i,3){
        sort(all(v[i]));
        if(cnt[i]%2){
            if(a==-1)
                a=i;
            else
                b=i;
            odd++;
        }
        else
            f = i;
    }
    if(odd==0){
        cout<<0<<endl;
        return;
    }
    if(cnt[f]==0){
        cout<<find(a,b,v)<<endl;
        return;
    }
    cout<<min(find(a,f,v)+find(b,f,v), find(a,b,v))<<endl;
}

signed main(){
    // #ifndef ONLINE_JUDGE
    //    freopen("./input.txt", "r", stdin);
    //    freopen("./output.txt", "w", stdout);
    // #endif
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    int t23 = 1, tt23 = 1; 
    // cin>>t23;
    init();
    while(tt23<=t23)
    {
        // cout<<"Case #"<<tt23<<": ";
        solve();
        tt23++;
    }
    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
}   