//Problem link : 
//done by sanath kumar
#include<bits/stdc++.h>	
#include <cstdlib>
using namespace std;
#define ll      			long long int
#define ld      			long double
#define mod     			1000000007
#define inf     			1e9
#define endl    			"\n"
#define pb      			push_back
#define vi 					vector<ll>
#define vs					vector<string>
#define pii 				pair<ll,ll>
#define ump 				unordered_map
#define mp 					make_pair
#define pq_max  			priority_queue<ll>
#define pq_min				priority_queue<ll,vector<ll>,greater<ll>>
#define all(v)				v.begin(),v.end()
#define ff 					first
#define ss					second
#define mid(l,r)    		(l+(r-l))/2
#define bitc(x)				__builtin_popcount(x)
#define loop(i,a,b)			for(int i=(a);i<=(b);i++)
#define looprev(i,a,b) 		for(int i=(a);i>=(b);i--)
#define iter(c,it)			for(__typeof(c.begin()) it=c.begin();it!=c.end();it++)
#define log(args...)		{string _s=#args;replace(_s.begin(),_s.end(),',',' ');stringstream _ss(_s);istream_iterator<string> _it(_ss);err(_it,args);}
#define logarr(arr,a,b)		for(int z=(a);z<=(b);z++) cout<<(arr[z])<<" ";cout<<endl;
template<typename T> T gcd(T a,T b) {if(b==0) return a; return gcd(b,a%b);}
template<typename T> T lcm(T a,T b) {return a*(b/gcd(a,b));}

void err(istream_iterator<string> it){}
template<typename T,typename... Args> 
void err(istream_iterator<string> it,T a,Args... args){
	cout<<*it<<" = "<<a<<endl;
	err(++it,args...);
}
void file_i_o(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
	#endif
}
ll *getfactorial(){
	int m=1e6;
	ll *fact=new ll[m+1];
	fact[0]=1;
	loop(i,1,m){
		fact[i]=(fact[i-1]*i)%mod;
	}
	// time complexity O(n)
	return fact;
}
vi *getfactors(){
	int n=2e5;
	vi *factors=new vi[n+5];
	loop(i,1,n){
		for(int j=i;j<=n;j+=i){
			factors[j].pb(i);
		}
	}
	// time complexity O(nlogn)
	return factors;
}
ll power(ll a,ll b,ll m=mod){
	if(b==0) return 1;
	ll smallans=power(a,b/2,m);
	ll ans=(smallans*smallans)%m;
	if(b%2==1){
		ans=(ans*a)%m;
	}
	return ans;
	// time complexity O(logb)
}
bool *isprime(){
	int m=1e6;
	bool *p=new bool[m];
	loop(i,0,m) p[i]=true;
	p[0]=false;
	p[1]=false;
	loop(i,2,sqrt(m)){
		for(int j=2*i;j<=m;j+=i){
			p[j]=false;
		}
	}
	// time complexity O(nloglogn)
	return p;
}
ll nCr(ll n,ll r,int m=mod){
	if(n<r) return 0; 
	ll dp[2][r+5];
	memset(dp,0,sizeof(dp));
	int i=1;
	loop(k,0,n) dp[k][0]=1;
	loop(k,1,n){
		loop(j,1,r){
			dp[i][j]=dp[i^1][j]+dp[i^1][j-1];
		}
		i=i^1;
	}
	// time complexity O(n*r) ,in worst case O(n^2)
	return dp[i^1][r];
}

//==================START==================
vi val[3];
ll twopoi(int x,int y){
	ll i=0,j=0,ans=1e15;
	while(i<val[x].size() and j<val[y].size()){
		ll p=val[y][j]-val[x][i];
		if(ans>abs(p)) ans=abs(p);
		if(p==0) break;
		if(p>0) i++;
		else j++;
	}
	return ans;
}
int main(){
	file_i_o();
	clock_t begin=clock();
	int n;
	cin>>n;
	
	loop(i,1,2*n){
		ll x;
		char c;
		cin>>x>>c;
		if(c=='R') val[0].pb(x);
		else if(c=='G') val[1].pb(x);
		else if(c=='B') val[2].pb(x); 
	}
	loop(i,0,2){
		if(val[i].size()!=0) sort(all(val[i]));
	}
	int r=val[0].size();
	int g=val[1].size();
	int b=val[2].size();
	if(r%2==0 and b%2==0 and g%2==0) cout<<0<<endl;
	else{
		ll ans=-1;
		if(r%2==0){
			ans=twopoi(1,2);
			if(val[0].size()!=0) ans=min(ans,twopoi(0,1)+twopoi(0,2));
		}
		else if(g%2==0){
			ans=twopoi(0,2);
			if(val[1].size()!=0) ans=min(ans,twopoi(1,0)+twopoi(1,2));
		}
		else{
			ans=twopoi(0,1);
			if(val[2].size()!=0) ans=min(ans,twopoi(2,1)+twopoi(0,2));
		}	
		cout<<ans<<endl;
	}
	#ifndef ONLINE_JUDGE
		clock_t end=clock();
		cout<<"\n\n\nExecuted in :"<<double(end-begin)/CLOCKS_PER_SEC*1000<<" ms"<<endl;
	#endif
}
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define int long long
#define ll long long
#define F(type, i, a, b, incr) for (type i = a; i <= (type)(b); i += (type)(incr))
#define RF(type, i, a, b, decr) for (type i = a; i >= (type)(b); i -= (type)(decr))
#define sz(a) sizeof(a)
#define deb(a) cerr << " [" << #a << "->" << a << "] "
#define next_line cerr << '\n'
#define all(a) a.begin(), a.end()
#define iter(it, s) for (auto it = s.begin(); it != s.end(); it++)
#define setbits(x) __builtin_popcountll(x)
using namespace __gnu_pbds;
using namespace std;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;

int INF = 1e18;

int Get(char color){
    if(color == 'R')
        return 0;
    if(color == 'G')
        return 1;
    return 2;
}

void solve()
{
    int n;
    cin >> n;
    n *= 2LL;
    // R, G, B
    vector<vector<int> > dogs(3);
    F(int, i, 0, n - 1, 1){
        int a;
        char c;
        cin >> a >> c;
        dogs[Get(c)].push_back(a);
    }
    F(int, i, 0, 2, 1){
        dogs[i].push_back(-INF);
        dogs[i].push_back(INF);
        sort(all(dogs[i]));
    }
    sort(all(dogs), [](vector<int> &a, vector<int> &b){
        return ((int)(a.size()) & 1) < ((int)(b.size()) & 1);
    });
    // 1->even
    if((((int)(dogs[1].size()) & 1)) == 0){
        cout << "0\n";
        return;
    }
    int min_1_2 = INF;
    vector<pii> min_0_1;
    vector<pii> min_0_2;
    for(int v: dogs[1]){
        if((v == (-INF)) || (v == (INF)))
            continue;
        auto p1 = lower_bound(all(dogs[2]), v);
        auto p2 = upper_bound(all(dogs[2]), v);
        p2 --;
        min_1_2 = min({min_1_2, (*p1) - v, v - (*p2)});
    }
    // deb(min_1_2);
    int z_s = dogs[0].size();
    F(int, i, 0, z_s - 1, 1){
        int v = dogs[0][i];
        if((v == (-INF)) || (v == (INF)))
            continue;
        auto p1 = lower_bound(all(dogs[1]), v);
        auto p2 = upper_bound(all(dogs[1]), v);
        p2 --;
        min_0_1.push_back(pii(min((*p1) - v, v - (*p2)), i));

        auto p3 = lower_bound(all(dogs[2]), v);
        auto p4 = upper_bound(all(dogs[2]), v);
        p4 --;
        min_0_2.push_back(pii(min((*p3) - v, v - (*p4)), i));
    }
    int oth_poss = INF;
    if(!min_0_1.empty()){
        sort(all(min_0_1));
        sort(all(min_0_2));
        if(min_0_1[0].second == min_0_2[0].second){
            oth_poss = min(oth_poss, min_0_1[0].first + min_0_2[1].first);
            oth_poss = min(oth_poss, min_0_2[0].first + min_0_1[1].first);
        }else{
            oth_poss = min(oth_poss, min_0_1[0].first + min_0_2[0].first);
        }
    }
    // deb(oth_poss);
    int ans = min(min_1_2, oth_poss);
    cout << ans << '\n';
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
        // freopen("input.txt", "r", stdin);
        // freopen("output.txt", "w", stdout);
        // freopen("Debug.txt", "w", stderr);
    #else
    #endif
    // cout << fixed << setprecision(10);
    int _t = 1;
    // cin>>_t;
    F(int, i, 1, _t, 1){
        // cout << "Case #" << i << ": ";
        solve();
    }
}
