#include <bits/stdc++.h>
using namespace std;
#define rep(i, n)  for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define pb emplace_back
#define lb(v,k) (lower_bound(all(v),(k))-v.begin())
#define ub(v,k) (upper_bound(all(v),(k))-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
#define dame(a) {out(a);return 0;}
#define decimal cout<<fixed<<setprecision(15);
#define all(a) a.begin(),a.end()
#define rsort(a) {sort(all(a));reverse(all(a));}
#define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());}
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef tuple<ll,ll,ll,ll> PPP;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vvvvi=vector<vvvi>;
using vp=vector<P>;
using vvp=vector<vp>;
using vb=vector<bool>;
using vvb=vector<vb>;
const ll inf=1001001001001001001;
const ll INF=1001001001;
const ll mod=1000000007;
const double eps=1e-10;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void yesno(T b){if(b)out("yes");else out("no");}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
template<class T> void YESNO(T b){if(b)out("YES");else out("NO");}
template<class T> void outset(T s){auto itr=s.begin();while(itr!=s.end()){if(itr!=s.begin())cout<<' ';cout<<*itr;itr++;}cout<<'\n';}
void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll modpow(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}

int main(){
    ll n;cin>>n;
    vvi v(3);
    rep(i,n*2){
        ll a;char c;cin>>a>>c;
        if(c=='R')v[0].pb(a);
        if(c=='G')v[1].pb(a);
        if(c=='B')v[2].pb(a);
    }
    rep(i,3)sort(all(v[i]));
    vi tmp;
    rep(i,3)if(v[i].size()%2)tmp.pb(i);
    if(tmp.size()==0)dame(0);
    ll ans=inf;
    vi t=v[tmp[1]];
    for(ll x:v[tmp[0]]){
        ll l=lb(t,x);
        if(l!=t.size())chmin(ans,t[l]-x);
        if(l)chmin(ans,x-t[l-1]);
    }
    ll a=tmp[0],b=tmp[1],c=3-a-b;
    if(v[c].size()){
        vp d,e;
        rep(i,v[c].size()){
            ll mi=inf;
            ll l=lb(v[a],v[c][i]);
            if(l!=v[a].size())chmin(mi,v[a][l]-v[c][i]);
            if(l)chmin(mi,v[c][i]-v[a][l-1]);
            d.pb(mi,i);
            mi=inf;l=lb(v[b],v[c][i]);
            if(l!=v[b].size())chmin(mi,v[b][l]-v[c][i]);
            if(l)chmin(mi,v[c][i]-v[b][l-1]);
            e.pb(mi,i);
        }
        sort(all(d));sort(all(e));
        if(d[0].se!=e[0].se)chmin(ans,d[0].fi+e[0].fi);
        else{
            chmin(ans,d[1].fi+e[0].fi);
            chmin(ans,d[0].fi+e[1].fi);
        }
    }
    out(ans);
}
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define _2n (n << 1)
typedef long long ll;

vector<ll> R, G, B;

ll find(vector<ll>& a, vector<ll>& b, vector<ll>& another)
{
    ll mn = 0x3f3f3f3f3f3f3f3f;
    for (ll x : a) {
        auto it = lower_bound(b.begin(), b.end(), x);
        if (it != b.end())
            mn = min(mn, abs(*it - x));
        if (it != b.begin())
            mn = min(mn, abs(*(--it) - x));
    }
    ll mn1, mn2;
    mn1 = mn2 = 0x3f3f3f3f3f3f3f3f;
    for (ll x : a) {
        auto it = lower_bound(another.begin(), another.end(), x);
        if (it != another.end())
            mn1 = min(mn1, abs(*it - x));
        if (it != another.begin())
            mn1 = min(mn1, abs(*(--it) - x));
    }
    for (ll x : b) {
        auto it = lower_bound(another.begin(), another.end(), x);
        if (it != another.end())
            mn2 = min(mn2, abs(*it - x));
        if (it != another.begin())
            mn2 = min(mn2, abs(*(--it) - x));
    }
    return min(mn, mn1 + mn2);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    int n, i;
    ll a;
    char c;
    cin >> n;
    for (i = 0; i < _2n; i++) {
        cin >> a >> c;
        if (c == 'R')
            R.push_back(a);
        else if (c == 'G')
            G.push_back(a);
        else
            B.push_back(a);
    }
    sort(R.begin(), R.end());
    sort(G.begin(), G.end());
    sort(B.begin(), B.end());
    if (R.size() % 2 == 0 && G.size() % 2 == 0 && B.size() % 2 == 0)
        cout << 0 << endl;
    else if (R.size() % 2 == 0)
        cout << find(G, B, R) << endl;
    else if (G.size() % 2 == 0)
        cout << find(R, B, G) << endl;
    else
        cout << find(R, G, B) << endl;
    return 0;
}