#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast")
/*#pragma comment(linker, "/stack:200000000")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")*/
/*#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,fma")*/
using namespace std;
using ll = long long int;
using ld = long double;
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define lc v<<1
#define rc v<<1|1
#define fast_io ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
const int N=5e5+10,LN=18,M=1.1e7+10,SQ=350,BS=737,inf=1e9+10;
const ll INF=1e15;
const int MH=1000696969,MOD=1000000007 /*998244353*/;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using pll=pair<ll,ll>;
using pii=pair<int,int>;
#define ordered_set tree<pll, null_type,less<pll>, rb_tree_tag,tree_order_statistics_node_update>
ll pow(ll x, ll y, ll mod){
    ll ans=1;
    while (y != 0) {
        if (y & 1) ans = ans * x % mod;
        y >>= 1;
        x = x * x % mod;
    }
    return ans;
}
ll n,cnt[3],ls[3],mn[3],ans;
pll a[N];
int main(){
    fast_io;
    cin >> n;
    n*=2;
    mn[0]=mn[1]=mn[2]=INF;
    for(ll i=1; i<=n; i++){
        cin >> a[i].F;
        char C;
        cin >> C;
        if(C=='G') a[i].S=1;
        if(C=='B') a[i].S=2;
        cnt[a[i].S]++;
    }
    sort(a+1,a+n+1);
    for(ll i=1; i<=n; i++){
        ls[a[i].S]=i;
        for(ll j=0; j<3; j++){
            if(j!=a[i].S && ls[j]){
                ll k=3-a[i].S-j;
                mn[k]=min(mn[k],a[i].F-a[ls[j]].F);
            }
        }
    }
    if(cnt[0]%2==1 && cnt[1]%2==1) ans=min(mn[2],mn[1]+mn[0]);
    if(cnt[0]%2==1 && cnt[2]%2==1) ans=min(mn[1],mn[2]+mn[0]);
    if(cnt[2]%2==1 && cnt[1]%2==1) ans=min(mn[0],mn[1]+mn[2]);
    cout << ans << '\n';
    return 0;
}


/*Be patient && calm!
All our dreams can come true, if we have the courage to pursue them.
I fear not the man who has practiced 10,000 kicks once, but I fear the man who has practiced one kick 10,000 times.
Do not pray for an easy life, pray for the strength to endure a difficult one.
When something is important enough, you do it even if the odds are not in your favor.
First, solve the problem. Then, write the code.
Fix the cause, not the symptom.
Simplicity is the soul of efficiency.
Make it work, make it right, make it fast.
No matter how hard it gets, NEVER GIVE UP*/
// set<int>::iterator it;
// set<int>::reverse_iterator it;
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007
#define Max 10000000000000000
int fact(int n)
{
    int res = 1;
    for (int i = 2; i <= n; i++)
        res = res * i;
    return res;
}
ll f1(vector<ll> &v1,vector<ll> &v2){
	int sz=v1.size();
	ll ans=1e18;
	for(int i=0;i<sz;i++){
		auto ind=lower_bound(v2.begin(),v2.end(),v1[i])-v2.begin();
		if(ind<(int)v2.size()&&abs(v2[ind]-v1[i])<ans)
			ans=abs(v2[ind]-v1[i]);
		if(ind>0&&abs(v2[ind-1]-v1[i])<ans)
			ans=abs(v2[ind-1]-v1[i]);
	}
	return ans;
}

ll f(vector<ll> v1,vector<ll> v2,vector<ll> v3){
	return min(f1(v2,v3),f1(v1,v2)+f1(v1,v3));
}
void solve(){
	int n;
	cin>>n;
	vector<ll> v[3];
	for(int i=0;i<2*n;i++){
		ll a;
		cin>>a;
		char c;
		cin>>c;
		if(c=='R')
			v[0].push_back(a);
		if(c=='G')
			v[1].push_back(a);
		if(c=='B')
			v[2].push_back(a);
	}
	sort(v[0].begin(),v[0].end());
	sort(v[1].begin(),v[1].end());
	sort(v[2].begin(),v[2].end());
	int l0=v[0].size(),l1=v[1].size(),l2=v[2].size();
	if(l1%2==0&&l2%2==0&&l0%2==0){
		cout<<"0\n";
		return;
	}
	if(l0%2==0){
		cout<<f(v[0],v[1],v[2]);
	}
	else if(l1%2==0){
		cout<<f(v[1],v[0],v[2]);
	}
	else if(l2%2==0){
		cout<<f(v[2],v[0],v[1]);
	}
}
int main(){
// 	ios::sync_with_stdio(false);
// 	cout.tie(0);
// 	cin.tie(NULL);
	int t;
	// cin>>t;
	t=1;
    while(t--) {
    	// int n;
    	// cin>>n;
    	solve();
	}
}