#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define Pr pair<ll,ll>
#define Tp tuple<ll,ll,ll>
using Graph = vector<vector<int>>;

ll mod = 1000000007;

int main() {
    ll N; cin >> N;
    vector<char> col = {'R','G','B'};
    vector<vector<ll>> cute(3);
    rep(i,2*N){
        ll a; char c; cin >> a >> c;
        rep(j,3){
            if(c==col[j]) cute[j].push_back(a);
        }
    }
    bool even[3];
    rep(i,3){
        cute[i].push_back(2e18);
        cute[i].push_back(-2e18);
        sort(cute[i].begin(),cute[i].end());
        if(cute[i].size()%2==0) even[i] = true;
        else even[i] = false;
    }
    if(even[0]&&even[1]&&even[2]){
        cout << 0 << endl; return 0;
    }
    ll md[3];
    rep(i,3) md[i] = 2e18;
    rep(i,3){
        rep(j,3){
            if(i<=j) continue;
            int k = 3-i-j;
            int M = cute[i].size()-2;
            for(int l=1;l<=M;l++){
                ll c = cute[i][l];
                int s = lower_bound(cute[j].begin(),cute[j].end(),c)-cute[j].begin();
                md[k] = min({md[k],abs(c-cute[j][s]),abs(c-cute[j][s-1])});
            }
        }
    }
    ll ans = 2e18;
    rep(i,3){
        if(even[i]) ans = min(ans,md[i]);
        //cout << md[i] << endl;
        rep(j,3){
            if(i<=j) continue;
            if(!even[i]&&!even[j]) ans = min(ans,md[i]+md[j]);
        }
    }
    cout << ans << endl;
}
/*...................................................................*
 *............___..................___.....____...______......___....*
 *.../|....../...\........./|...../...\...|.............|..../...\...*
 *../.|...../.....\......./.|....|.....|..|.............|.../........*
 *....|....|.......|...../..|....|.....|..|............/...|.........*
 *....|....|.......|..../...|.....\___/...|___......../....|..___....*
 *....|....|.......|.../....|...../...\.......\....../.....|./...\...*
 *....|....|.......|../_____|__..|.....|.......|..../......|/.....\..*
 *....|.....\...../.........|....|.....|.......|.../........\...../..*
 *..__|__....\___/..........|.....\___/...\___/.../..........\___/...*
 *...................................................................*
 */
 
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 1000000000000000000

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int tt=1;
    //cin >> tt;
    while(tt--)
    {
        int n;
        cin >> n;
        vector<int> r,g,b;
        int cr=0,cg=0,cb=0;
        for(int i=0;i<2*n;i++)
        {
            int x;
            cin >> x;
            string c;
            cin >> c;
            if(c=="R")
            {
                r.push_back(x);
                cr++;
            }
            else if(c=="G")
            {
                g.push_back(x);
                cg++;
            }
            else
            {
                b.push_back(x);
                cb++;
            }
        }
        if(cr%2==0 && cg%2==0 && cb%2==0)
        {
            cout << 0 << '\n';
        }
        else
        {
            if(cr%2==1 && cg%2==1)
            {
                vector<int> t=b;
                b=g;
                g=t;
                cg=g.size();
                cb=b.size();
            }
            else if(cb%2==1 && cg%2==1)
            {
                vector<int> t=r;
                r=g;
                g=t;
                cg=g.size();
                cr=r.size();
            }
            //Now, cr%2==1 and cb%2==1
            sort(r.begin(),r.end());
            sort(g.begin(),g.end());
            sort(b.begin(),b.end());
            int closerg=INF,closebg=INF,closerb=INF;
            int j=0;
            for(int i=0;i<cr;i++)
            {
                while(j<cg && g[j]<r[i])
                    j++;
                j--;
                if(j<cg)
                    closerg=min(closerg,abs(r[i]-g[j]));
                if(j+1<cg)
                    closerg=min(closerg,abs(r[i]-g[j+1]));
            }
            j=0;
            for(int i=0;i<cb;i++)
            {
                while(j<cg && g[j]<=b[i])
                    j++;
                j--;
                if(j<cg)
                    closebg=min(closebg,abs(b[i]-g[j]));
                if(j+1<cg)
                    closebg=min(closebg,abs(b[i]-g[j+1]));
            }
            j=0;
            for(int i=0;i<cr;i++)
            {
                while(j<cb && b[j]<=r[i])
                    j++;
                j--;
                if(j<cb)
                    closerb=min(closerb,abs(r[i]-b[j]));
                if(j+1<cb)
                    closerb=min(closerb,abs(r[i]-b[j+1]));
            }
            cout << min(closerg+closebg,closerb) << '\n';
        }
    }
    return 0;
}