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

vector<ll>v[3];
int sz[3];
int baad;
ll calc(int c1,int c2,bool check=false)
{

    ll ansmin = 1e18;
    ll a1,a2;
    int pos, anspos = -1,i;
    for(i=0;i<sz[c1];i++)
    {
        a1 = v[c1][i];
        pos = upper_bound(v[c2].begin(), v[c2].end(), a1) - v[c2].begin();
        if(pos != sz[c2])
        {
            a2 = v[c2][pos];
            if(abs(a1-a2) < ansmin)
            {
                if(!(check && baad == pos))
                {
                    ansmin = abs(a1-a2);
                    anspos = pos;
                }
            }
        }
        if(pos > 0)
        {
            pos--;
            a2 = v[c2][pos];
            if(abs(a1-a2) < ansmin)
            {
                if(!(check && baad == pos))
                {
                    ansmin = abs(a1-a2);
                    anspos = pos;
                }
            }
        }
    }
    baad = anspos;
    return ansmin;

}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n,i,j,k;
    ll a;
    char c;
    cin >> n;
    n *= 2;
    for(i=1;i<=n;i++)
    {
        cin >> a >> c;
        if(c == 'R') v[0].push_back(a);
        else if(c == 'G') v[1].push_back(a);
        else v[2].push_back(a);
    }
    for(i=0;i<3;i++) {
        sz[i] = v[i].size();
        sort(v[i].begin(), v[i].end());
    }


    if(sz[0]%2==0 && sz[1]%2==0 && sz[2]%2==0)
    {
        cout << 0 << endl;
        return 0;
    }

    ll ans1, ans2, ansmin;
    if(sz[0]%2 == 0) {
        ansmin = calc(1,2);
        ans1 = (calc(1,0) + calc(2,0,true));
        ans2 = (calc(2,0) + calc(1,0,true));
        ansmin = min(ansmin, min(ans1, ans2));
    }
    else if(sz[1]%2 == 0) {
        ansmin = calc(0,2);
        ans1 = (calc(0,1) + calc(2,1,true));
        ans2 = (calc(2,1) + calc(0,1,true));
        ansmin = min(ansmin, min(ans1, ans2));
    }
    else {
        ansmin = calc(0,1);
        ans1 = (calc(0,2) + calc(1,2,true));
        ans2 = (calc(1,2) + calc(0,2,true));
        ansmin = min(ansmin, min(ans1, ans2));
    }
    cout << ansmin << endl;





}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define pii pair <int,int>
#define pll pair <ll,ll>
#define testcase() int t; cin>>t; while(t--)
#define forn(i,n) for(int i=0;i<n;i++)
#define forn1(i,n) for(int i=1;i<=n;i++)
#define vll vector <ll>
#define vi vector <int>
#define all(v) v.begin(),v.end()

ll M = 1e9 + 7;
double pi = acos(-1.0);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll powerm(ll x,ll y){ ll res=1; while(y){ if(y&1) res=(res*x)%M; y=y>>1; x=(x*x)%M;} return res%M; }
ll power(ll x,ll y){ ll res=1; while(y){ if(y&1) res=(res*x); y=y>>1; x=(x*x);} return res; }
ll gcd(ll a,ll b){if(b>a)return gcd(b,a); if(b==0)return a; return gcd(b,a%b);}

ll solve(vll a, vll b)
{
	ll ans = 1e18;
	int id1 = 0, id2 = 0;
	int m = a.size(), n = b.size();
	while( id1 < m && id2 < n )
	{
		ans = min( ans , abs(a[id1]-b[id2]) );
		if( a[id1] < b[id2] ) id1++;
		else id2++;
	}
	return ans;
}

ll solve1(vll a,vll b,vll c)
{
	ll ans1 = 1e18; int id = -1;
	int id1 = 0, id2 = 0;
	int m = a.size(), n = b.size();
	while( id1 < m && id2 < n )
	{
		if( ans1 > abs(a[id1]-b[id2]) )
		{
			ans1 = abs(a[id1]-b[id2]);
			id = id1;
		}
		if( a[id1] < b[id2] ) id1++;
		else id2++;
	}
	id1 = 0; id2 = 0;
	n = c.size();
	ll ans2 = 1e18;
	while( id1 < m && id2 < n )
	{
		if( id1 == id ){ id1++; continue; }
		if( ans2 > abs(a[id1]-c[id2]) )
		{
			ans2 = abs(a[id1]-c[id2]);
		}
		if( a[id1] < c[id2] ) id1++;
		else id2++;
	}
	return ans1 + ans2;
}


int main() {
	
	//ifstream cin("input.txt");
	//ofstream cout("output.txt");
	ios_base::sync_with_stdio(false); 
    cin.tie(NULL); 
	
	int n; cin>>n;
	map <char,vll> m;
	forn(i,n*2)
	{
		ll a; char c;
		cin>>a>>c;
		m[c].pb(a);
	}
	sort(all(m['R']));
	sort(all(m['G']));
	sort(all(m['B']));

	if( m['R'].size()%2 == 0 && m['G'].size()%2 == 0 && m['B'].size()%2 == 0 )
	{
		cout<<0; return 0;
	}
	string s;
	if(m['R'].size()%2 == 0) s = "RBG";
	if(m['G'].size()%2 == 0) s = "GRB";
	if(m['B'].size()%2 == 0) s = "BRG";
	ll h1 = solve(m[s[1]],m[s[2]]);
	ll h2 = solve1(m[s[0]],m[s[1]],m[s[2]]);
	ll h3 = solve1(m[s[0]],m[s[2]],m[s[1]]);
	cout << min( h1 , min( h2 , h3 ) );
	
	return 0;
}
