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




int main() {
    
    ll N;
    cin>>N;
    vector<pair<ll,char> > vp(2*N);
    
    ll n;
    char c;
    
    map<char,ll> mp;
    
    for(int i=0;i<2*N;i++){
        cin>>n;
        cin>>c;
        vp[i]=make_pair(n,c);
        mp[c]++;
    }
    vector<pair<char,int> > vcc;
    vector<pair<char,int> > vce;
    for(auto a:mp){
        
       //cout<<a.first<<" "<<a.second<<endl;
        //a.second=(a.second)%2;
       // cout<<a.first<<" "<<a.second<<endl;
        if(a.second%2==1){
            vcc.push_back(make_pair(a.first,0));
        }else{
            vce.push_back(make_pair(a.first,0));
        }
    }
    /*
    for(auto a:vcc){
        cout<<a.first<<" "<<a.second;
        
    }
    for(auto a:vce){
        cout<<a.first<<" "<<a.second;
        
    }
    */
    
    sort(vp.begin(),vp.end(),[](pair<ll,char> a,pair<ll,char> b){
        return a.first<b.first;
    });
    /*
    for(auto a: mp){
        cout<<a.first<<" "<<a.second<<endl;
    }
    */
    
    vector<pair<ll,pair<char,char> > > vc;
    
    for(int i=0;i<2*N-1;i++){
        vc.push_back(pair<ll,pair<char,char> >(abs(vp[i].first-vp[i+1].first),pair<char,char>(vp[i].second,vp[i+1].second)));
    }
    sort(vc.begin(),vc.end(),[](pair<ll,pair<char,char> > a,pair<ll,pair<char,char> > b){
        return a.first<b.first;
    });
    ll ans=0;
    
    ll ans1=0;
    ll ans2=0;
    ll cnt=0;
    bool flag1=false;
    bool flag2=false;

    //cout<<mp['R']<<" "<<mp['B']<<" "<<mp['C']<<endl;

    
    if(mp['R']%2==0&&mp['B']%2==0&&mp['G']%2==0){
        ans=0;
    }else{
       // cout<<"---"<<endl;
        for(int i=0;i<2*N-1;i++){
            if(((vc[i].second.first==vcc[0].first)&&(vc[i].second.second==vcc[1].first))||(vc[i].second.first==vcc[1].first&&vc[i].second.second==vcc[0].first)){
               //cout<<vc[i].first<<endl;
                if(cnt==0){
                   // cout<<"0000"<<endl;
                    ans1+=vc[i].first;
                    flag1=true;
                }
                cnt++;
              
            }else{
                if(((vc[i].second.first==vce[0].first)&&(vc[i].second.second==vcc[0].first))||(vc[i].second.first==vcc[0].first&&vc[i].second.second==vce[0].first)){
                   
                    if(vcc[0].second==0){
                        ans2+=vc[i].first;
                    }
                    vcc[0].second++;
                   
                }else if(((vc[i].second.first==vce[0].first)&&(vc[i].second.second==vcc[1].first))||(vc[i].second.first==vcc[1].first&&vc[i].second.second==vce[0].first)){
                 
                    if(vcc[1].second==0){
                        ans2+=vc[i].first;
                    }
                    vcc[1].second++;
                }
                
            }
            
        }
        if(vcc[0].second>0&&vcc[1].second>0){
            flag2=true;
        }
        if(flag1==true&&flag2==false){
            ans=ans1;
        }else if(flag1==false&&flag2==true){
            ans=ans2;
        }else{
            ans=min(ans1,ans2);
        }
        
        
    }
    
    cout<<ans<<endl;
  
}

#include <bits/stdc++.h>          
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repp(i, st, en) for (ll i = (ll)st; i < (ll)(en); i++)
#define repm(i, st, en) for (ll i = (ll)st; i >= (ll)(en); i--)
#define all(v) v.begin(), v.end()
void chmax(ll &x, ll y) {x = max(x,y);}
void chmin(ll &x, ll y) {x = min(x,y);}
void Yes() {cout << "Yes" << endl; exit(0);}
void No() {cout << "No" << endl; exit(0);}
template<class in_Cout> void Cout(in_Cout x) {cout << x << endl; exit(0);}
template<class in_vec_cout>
void vec_cout(vector<in_vec_cout> vec) {
    for (in_vec_cout res : vec) {cout << res << " ";}
    cout << endl;
}

const ll inf = 1e18;
const ll mod = 1e9 + 7;

ll id(char c) {
    if (c=='R') return 0;
    if (c=='G') return 1;
    return 2;
}

bool is_end(vector<vector<ll>> vec) {
    for (auto a : vec) {
        if (a.size()%2) return false;
    }
    return true;
}

int main() {
    ll N; cin >> N;
    vector<vector<ll>> a(3);
    rep(i,2*N) {
        ll x; char c;
        cin >> x >> c;
        a[id(c)].push_back(x);
    }

    ll cnt0 = 0;
    rep(i,3) if (a[i].size()==0) cnt0++;

    if (cnt0==2) Cout(0);

    if (is_end(a)) Cout(0);
    rep(i,2) if (a[i].size()%2==0) swap(a[i],a[2]);
    rep(i,3) sort(all(a[i]));

    ll ans = inf;
    rep(i,a[0].size()) {
        ll x = a[0][i];
        ll l = -1, r = a[1].size();
        ll res = inf;
        while (r-l>1) {
            ll m = (l+r)/2;
            if (a[1][m]<=x) l = m;
            else r = m;
        }
        if (r<a[1].size()) chmin(res,abs(a[1][r]-x));
        if (l>=0) chmin(res,abs(a[1][l]-x));
        chmin(ans,res);
        if (ans==0) Cout(0);
    }

    if (cnt0==1) Cout(ans);

    vector<ll> X, Y;
    rep(i,a[2].size()) {
        ll x = a[2][i];
        rep(j,2) {
            ll l = -1, r = a[j].size();
            ll res = inf;
            while (r-l>1) {
                ll m = (l+r)/2;
                if (a[j][m]<=x) l = m;
                else r = m;
            }
            if (r<a[j].size()) chmin(res,abs(a[j][r]-x));
            if (l>=0) chmin(res,abs(a[j][l]-x));
            if (j%2) X.push_back(res);
            else Y.push_back(res);
        }
    }
    sort(all(X)); sort(all(Y));
    chmin(ans,X[0]+Y[0]);
    Cout(ans);
}