#include <bits/stdc++.h>
#define ll long long
#define lll long long
#define mp make_pair
#define pb push_back
ll inf = 1000000000000000000;
#define deci(n) fixed << setprecision(n)
#define vll vector<ll>
#define vpll vector<pair<ll, ll>>
#define umll unordered_map<ll, ll>
#define mll map<ll, ll>
//#define f first
//#define s second
#define mt make_tuple
#define g(i, name) get<i>(name)

using namespace std;
 
struct custom_hash
{
    static uint64_t splitmix64(uint64_t x)
    {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }
 
    size_t operator()(uint64_t x) const
    {
        static const uint64_t FIXED_RANDOM = std::chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
 
struct hash_pair
{
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2> &p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};

ll p = 1e9 + 7;

//*****************CODE STARTS **************************************
//#define f first
ll mingap(vector <ll> & r, ll x, vector <ll> & g, ll y){
    if(x==0 || y==0)return inf;
    ll minrg=inf;
    ll j=0;
        for(ll i=0; i<x; i++){
            while(j!=y-1 && g[j+1]<=r[i])j++;
            minrg = min(minrg, abs(r[i]-g[j]));
            if(g[j]<=r[i] && j!=y-1)j++;
            minrg = min(minrg, abs(r[i]-g[j]));
        }
        return minrg;
}


int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
 
 
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    ll n; cin >> n; n*=2;
    vector <ll> r, g, b;

    for(ll i=0; i<n; i++){
        ll x; cin >> x; char c; cin >> c;
        if(c=='R')r.pb(x);
        else if(c=='G')g.pb(x);
        else b.pb(x);
    }

    sort(r.begin(), r.end());
    sort(g.begin(), g.end());
    sort(b.begin(), b.end());

    if(r.size()%2==0 && g.size()%2==0)cout << 0;
    else if(r.size()%2==0){
        ll minrg=inf, mingb=inf, minbr=inf;
        ll x =r.size(), y=g.size(), z=b.size();

        minrg = mingap(r, x, g, y);
        mingb = mingap(g, y, b, z);
        minbr = mingap(b, z, r, x);
        //cout << minrg << " " << mingb << " " << minbr <<"\n";
        cout << min(mingb, minrg+minbr);
    }
    else{
        if(g.size()%2==0)swap(r, g);
        else swap(r, b);

        ll minrg=inf, mingb=inf, minbr=inf;
        ll x =r.size(), y=g.size(), z=b.size();

        minrg = mingap(r, x, g, y);
        mingb = mingap(g, y, b, z);
        minbr = mingap(b, z, r, x);
        //cout << minrg << " " << mingb << " " << minbr <<"\n";
        cout << min(mingb, minrg+minbr);
    }
}


#include <bits/stdc++.h>

typedef long long ll;
using namespace std;
vector<ll> a[3];

bool comp(vector<ll> &x, vector<ll> &y){
    return ((int)x.size()&1)<((int)y.size()&1);
}

int main(){
    int n;
    scanf("%d", &n);
    for (int i=0;i<n*2;i++){
        char col;
        ll val;
        scanf("%lld %c", &val, &col);
        if (col=='R') a[0].push_back(val);
        if (col=='G') a[1].push_back(val);
        if (col=='B') a[2].push_back(val);
    }
    for (int i=0;i<3;i++) sort(a[i].begin(), a[i].end());
    sort(a, a+3, comp);
    if (!(a[2].size()&1)){
        printf("0\n"); return 0;
    }
    ll ans1=1e18, ans2=1e18;
    for (auto &x:a[1]){
        auto iter = lower_bound(a[2].begin(), a[2].end(), x);
        if (iter!=a[2].end()) ans1 = min(ans1, (*iter)-x);
        if (iter!=a[2].begin()) ans1 = min(ans1, x-(*(--iter)));
    }
    if (a[0].empty()){
        printf("%lld\n", ans1); return 0;
    }

    vector<pair<ll, ll>> tmp(a[0].size(), {1e18, 1e18});

    for (int i=0;i<(int)a[0].size();i++){
        auto x = a[0][i];
        auto iter = lower_bound(a[1].begin(), a[1].end(), x);
        if (iter!=a[1].end()) tmp[i].first = (*iter)-x;
        if (iter!=a[1].begin()) tmp[i].first = min(tmp[i].first, x-(*(--iter)));
    }

    for (int i=0;i<(int)a[0].size();i++){
        auto x = a[0][i];
        auto iter = lower_bound(a[2].begin(), a[2].end(), x);
        if (iter!=a[2].end()) tmp[i].second = (*iter)-x;
        if (iter!=a[2].begin()) tmp[i].second = min(tmp[i].second, x-(*(--iter)));
    }

    int idx = 0, idx2 = 0;
    for (int i=0;i<(int)a[0].size();i++){
        if (ans2>min(tmp[i].first, tmp[i].second)){
            ans2 = min(tmp[i].first, tmp[i].second), idx = i;
            if (tmp[i].first<tmp[i].second) idx2 = 1;
            else idx2 = 2;
        }
    }
    ll tmp2 = 1e18;
    for (int i=0;i<(int)a[0].size();i++) if (i!=idx){
        if (idx2==1) tmp2 = min(tmp2, tmp[i].second);
        if (idx2==2) tmp2 = min(tmp2, tmp[i].first);
    }
    ans2 += tmp2;
    ll ans3;
    if (idx2==1) ans3 = tmp[idx].second;
    else ans3 = tmp[idx].first;
    tmp2 = 1e18;
    for (int i=0;i<(int)a[0].size();i++) if (i!=idx){
        if (idx2==2) tmp2 = min(tmp2, tmp[i].second);
        if (idx2==1) tmp2 = min(tmp2, tmp[i].first);
    }
    ans3 += tmp2;
    printf("%lld\n", min(ans1, min(ans2, ans3)));
    return 0;
}
