#include <bits/stdc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rep1(i,n) for (int i = 1; i <= (n); ++i)
#define repr(i,n) for (int i = (n)-1; i >= 0; --i)
#define rep1r(i,n) for (int i = (n); i > 0; --i)
#define bit(n,k) ((n>>k)&1) //nのk　bit目
#define vec(T) vector<T>
#define vvec(T) vector<vector<T>>
using ll = long long;
using P = pair<int,int>;
using Pll = pair<ll,ll>;


template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const ll llINF = 1LL << 60;
const int iINF = 1e9;

//------------------------------------------------


struct Solver{

    ll mindiff(vec(ll) a, vec(ll) b){
        ll res = llINF;

        ll idx=0;
        for(ll ai:a){
            while(idx<b.size()){
                ll bi = b[idx];
                chmin(res, abs(ai-bi));
                if(ai<=bi) break;
                idx++;
            }
        }
        return res;
    }


    void solve(){
        ll N;
        cin >> N;
        vec(ll) R,G,B;
        rep(i,2*N){
            ll ai;
            char ci;
            cin >>ai >> ci;
            if(ci=='R') R.push_back(ai);
            if(ci=='G') G.push_back(ai);
            if(ci=='B') B.push_back(ai);
        }

        if(R.size()%2==0 && G.size()%2==0 && B.size()%2==0){
            cout << 0 << endl;
            return;
        }


        sort(R.begin(),R.end());
        sort(G.begin(),G.end());
        sort(B.begin(),B.end());



        ll ans = llINF;
        if(R.size()%2==0){
            ans = mindiff(R,G)+mindiff(R,B);
            chmin(ans, mindiff(G,B));
        }else if(G.size()%2==0){
            ans = mindiff(R,G)+mindiff(G,B);
            chmin(ans, mindiff(R,B));
        }else{
            ans = mindiff(R,B)+mindiff(G,B);
            chmin(ans, mindiff(G,R));
        }
        cout << ans << endl;
    }
};


int main(){
    int testcasenum=1;
    //cin >> testcasenum;
    rep1(ti,testcasenum){
        Solver solver;
        solver.solve();
    }
    return 0;
}

#include<bits/stdc++.h>
#define FORz(i,t,n) for(int i=t;i<=n;i++)
#define FORf(i,n,t) for(int i=n;i>=t;i--)
#define SCC(x) scanf("%d",&x)
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
typedef long long ll;
using namespace std;
const int N=2e5+5;
const ll MOD=1e9+7;
// inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
//ll pow1(ll a,ll b){ll r=1;while(b){if(b&1)r=r*a;a=a*a;b>>=1;}return r;}
ll pow2(ll a,ll b,ll mod){ll r=1;while(b){if(b&1)r=r*a%mod;a=a*a%mod;b>>=1;}return r%mod;}
//ll gcd(ll a,ll b){ll m=a%b;while(m){a=b;b=m;m=a%b;}return b;}
void exgcd(ll a, ll b, ll &d, ll &x, ll &y){if(!b) { d = a; x = 1; y = 0;}else { exgcd(b, a % b, d, y, x); y -= x * (a / b); }}
ll inv(ll a){ll d, x, y;exgcd(a, MOD, d, x, y);return d == 1 ? (x + MOD) % MOD : -1;}struct node{ll mat[105][105];};node mul(node x,node y,int n,ll mod){node tmp;for(int i=0;i<n;i++)for(int j=0;j<n;j++){tmp.mat[i][j]=0;for(int k=0;k<n;k++)tmp.mat[i][j]+=(x.mat[i][k]*y.mat[k][j])%mod;tmp.mat[i][j]%=mod;}return tmp;}
// node matpow(node x,node y,ll num,int n,ll mod){while(num){if(num&1){y=mul(x,y,n,mod);}x=mul(x,x,n,mod);num=num>>1;}return y;}
//for(ll i=2;i<N-1;i++){if(!a[i]) b[++cnt]=i;for(ll j=1;j<=cnt&&i*b[j]<N;j++){a[i*b[j]]=1;if(i%b[j]==0) break;}}
//int find(int x){return a[x]==x?x:a[x]=find(a[x]);}
//void merge(int x,int y){a[find(y)]=a[find(x)];}
int n;
struct ty{
    ll a;
    char c;
    bool operator <(ty kk){
        return a<kk.a;
    }
}x[N];
char a[3];
int sum[500];
int w[500];
map<int,ll>mp;
int main()
{
    cin>>n;
    FORz(i,1,n<<1) cin>>x[i].a>>x[i].c,sum[x[i].c]++;
    int p=-1;
    if(sum['R']&1) a[++p]='R';else a[2]='R';
    if(sum['G']&1) a[++p]='G';else a[2]='G';
    if(sum['B']&1) a[++p]='B';else a[2]='B';
    if(!a[0]){
        cout<<0;
        return 0;
    }
    sort(x+1,x+(n<<1)+1);
    mp['R'+'B']=1e16,mp['R'+'G']=1e16,mp['B'+'G']=1e16;

    FORz(i,1,n<<1){
        w[x[i].c]=i;
        if(w['R']) mp['R'+x[i].c]=min(x[i].a-x[w['R']].a,mp['R'+x[i].c]);
        if(w['G']) mp['G'+x[i].c]=min(x[i].a-x[w['G']].a,mp['G'+x[i].c]);
        if(w['B']) mp['B'+x[i].c]=min(x[i].a-x[w['B']].a,mp['B'+x[i].c]);
    }
    cout<<min(mp[a[0]+a[1]],mp[a[0]+a[2]]+mp[a[1]+a[2]]);
}
