#include <bits/stdc++.h>
#define REP(i,a,b) for (int i = (a); i <= (b); ++i)
#define PER(i,a,b) for (int i = (b); i >= (a); --i)
#define log2(x) (31-__builtin_clz(x))
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
#define mup(x,y) x = std::min(x,y)
#define Mup(x,y) x = max(x,y)
#define debug(x) cout << #x << " is " << x << endl
#define endl '\n'
using namespace std; using ll = long long; using ii = pair<int,int>; using iii = tuple<int,int,int>;
void solution(); int main() {ios::sync_with_stdio(0); cin.tie(0); solution();}
const int N = 200003;
int n;
vector<ll> r, g, b;

ll calc(vector<ll>& x, vector<ll>& y) {
    ll a = 0, b = 0, r = 10000000000000000;
    while (a < SZ(x) && b < SZ(y)) {
        if (x[a] < y[b]) mup(r, y[b]-x[a++]);
        else if (x[a] > y[b]) mup(r, x[a]-y[b++]);
        else return 0;
    }
    return r;
}

void input() {
    cin >> n;
    REP(i,1,2*n) {
        ll a; char c;
        cin >> a >> c;
        if (c == 'R') r.push_back(a);
        else if (c == 'G') g.push_back(a);
        else b.push_back(a);
    }
}
void solution() {
    input();
    sort(ALL(r)); sort(ALL(g)); sort(ALL(b));
    bool pr = SZ(r)&1, pg = SZ(g)&1, pb = SZ(g)&1;
    if (pr == 0 && pg == 0 && pb == 0) {cout << 0; return;}
    auto x = calc(r,g), y = calc(r,b), z = calc(g,b);
    if (SZ(r)%2 == 0) swap(x,z);
    else if (SZ(g)%2 == 0) swap(x,y);

    // cout << x << ' ' << y << ' ' << z << endl;
    cout << min(x,y+z);
}
#include<bits/stdc++.h>
#define ll long long
using namespace std;

ll MX=100000000000000000LL;

ll ck(vector<ll> a, vector<ll> b)
{
    ll mn=MX;
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    for(int i=0;i<a.size();i++)
    {
        auto it=upper_bound(b.begin(), b.end(), a[i]);
        if(it!=b.end())
        {
            mn=min(mn, abs(*it-a[i]));
        }
        if(it!=b.begin())
        {
            it=prev(it);
            mn=min(mn, abs(*it-a[i]));
        }
    }
    return mn;

}

ll ck1(vector<ll> a, vector<ll> b, vector<ll> c)
{
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    sort(c.begin(), c.end());
    vector<ll> dl(a.size()), dr(a.size());
    for(int i=0;i<a.size();i++)
    {
        ll x=MX;
        auto it=upper_bound(b.begin(), b.end(), a[i]);
        if(it!=b.end())
        {
            x=min(x, abs(*it-a[i]));
        }
        if(it!=b.begin())
        {
            it=prev(it);
            x=min(x, abs(*it-a[i]));
        }
        if(i>0) dl[i]=min(x, dl[i-1]);
        else dl[i]=x;
    }
    for(int i=a.size()-1;i>=0;i--)
    {
        ll x=MX;
        auto it=upper_bound(b.begin(), b.end(), a[i]);
        if(it!=b.end())
        {
            x=min(x, abs(*it-a[i]));
        }
        if(it!=b.begin())
        {
            it=prev(it);
            x=min(x, abs(*it-a[i]));
        }
        if(i!=a.size()-1) dr[i]=min(x, dr[i+1]);
        else dr[i]=x;
    }
    
    ll mn=MX;
    for(int i=0;i<a.size();i++)
    {
        ll x=MX;
        auto it=upper_bound(c.begin(), c.end(), a[i]);
        if(it!=c.end())
        {
            x=min(x, abs(*it-a[i]));
        }
        if(it!=c.begin())
        {
            it=prev(it);
            x=min(x, abs(*it-a[i]));
        }
        ll y=MX;
        if(i>0) y=min(y, dl[i-1]);
        if(i+1<a.size()) y=min(y, dr[i+1]);
        mn=min(mn, x+y);
    }
    return mn;
}

int main()
{
    ll n;
    cin>>n;
    map<char, vector<ll> > mp;
    for(int i=1;i<=2*n;i++)
    {
        ll x;
        char c;
        cin>>x>>c;
        mp[c].push_back(x);
    }
    if(mp['B'].size()%2==0 and mp['R'].size()%2==0 and mp['G'].size()%2==0)
    {
        cout<<0<<endl;
        return 0;
    }
    if(mp['B'].size()%2==0)
    {
        ll x=ck(mp['R'], mp['G']);
        if(mp['B'].size()>=2)
            x=min(x, ck1(mp['B'], mp['R'], mp['G']));
        cout<<x<<endl;
        return 0;
    }

    if(mp['R'].size()%2==0)
    {
        ll x=ck(mp['B'], mp['G']);
        if(mp['R'].size()>=2)
        {
            x=min(x, ck1( mp['R'], mp['B'], mp['G'] ));
        }
        cout<<x<<endl;
        return 0;
    }

    if(mp['G'].size()%2==0)
    {
        ll x=ck(mp['R'], mp['B']);
        if(mp['G'].size()>=2)
        {
            x=min(x, ck1(mp['G'], mp['R'], mp['B']));
        }
        cout<<x<<endl;
        return 0;
    }

    return 0;
}