#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i=a; i<=b; i++)
#define repd(i, a, b) for(int i=a; i>=b; i--)
#define vi vector<int>
#define vll vector<long long>
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define fr first
#define sc second
#define vb vector<bool>

typedef long long ll;
typedef unsigned long long ull;

/*struct cmp {
    bool operator()(const pair<int, pii> &p1, const pair<int, pii> &p2) {
        return p1.fr > p2.fr;
    }
};*/

void google(){
	static int tc=1;
	cout<<"Case #"<<tc<<": ";
	tc++;
}

int n;

ll fun(vll &v1,vll &v2){
	ll ans=1e18;
	bool ok=0;
	if(v1.size()>v2.size()){
		swap(v1,v2);
		ok=1;
	}
	for(int i=0;i<v1.size();i++){
		int id=lower_bound(v2.begin(),v2.end(),v1[i])-v2.begin();
		ans=min(ans,abs(v2[id]-v1[i]));
		if(id!=0) id--;
		ans=min(ans,abs(v2[id]-v1[i]));
	}	
	if(ok) swap(v1,v2);
	return ans;
}

void solve(int tc = 1) {
	vll v[3];
	for(int i=0;i<2*n;i++){
		ll a;
		char ch;
		cin>>a>>ch;
		if(ch=='R'){
			v[0].pb(a);
		}
		else if(ch=='G'){
			v[1].pb(a);
		}	
		else{
			v[2].pb(a);
		}
	}
	
	ll ans=1e18;
	
	for(int i=0;i<3;i++) sort(v[i].begin(),v[i].end());
	
	if(v[0].size()%2==0 && v[1].size()%2==0 && v[2].size()%2==0){
		cout<<0<<endl;
		return;
	}
	
	if(v[0].size()%2==0){
		ans=min(ans,fun(v[1],v[2]));
		ans=min(ans,fun(v[1],v[0])+fun(v[2],v[0]));
	}
	else if(v[1].size()%2==0){
		ans=min(ans,fun(v[0],v[2]));
		ans=min(ans,fun(v[1],v[0])+fun(v[2],v[1]));			
	}
	else{
		ans=min(ans,fun(v[1],v[0]));
		ans=min(ans,fun(v[1],v[2])+fun(v[2],v[0]));			
	}
	cout<<ans<<endl;
}

int main() {
/*#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif*/
    ios::sync_with_stdio(false);
    cin.tie(0);
    //cout << setprecision(16) << fixed;
    int t = 1;
    // cin >> t;
    int tc=0;
    
    while (t--) {
    	cin>>n;
        tc++;
        solve(tc);
    }
}


#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2,fma,tune=native")

#define ll long long
#define int ll
#define ull unsigned ll
#define ld long double
#define rep(a) rep1(i,a)
#define rep1(i,a) rep2(i,0,a)
#define rep2(i,b,a) for(int i=(b); i<((int)(a)); i++)
#define rep3(i,b,a) for(int i=(b); i>=((int)(a)); i--)
#define all(a) a.begin(),a.end()
#define pii pair<int,int>
#define pb push_back
//#define inf 1010000000
#define inf 4000000000000000000
#define eps 1e-9
#define sz(a) ((int)a.size())
#define pow2(x) (1ll<<(x))
#define ceiling(a,b) (((a)+(b)-1)/(b))
#define print0(a) cout << (a) << ' '
#define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#ifdef i_am_noob
#define bug(...) cerr << "#" << __LINE__ << ' ' << #__VA_ARGS__ << "- ", _do(__VA_ARGS__)
template<typename T> void _do(vector<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";}
template<typename T> void _do(set<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";}
template<typename T> void _do(T && x) {cerr << x << endl;}
template<typename T, typename ...S> void _do(T && x, S&&...y) {cerr << x << ", "; _do(y...);}
#else
#define bug(...) 826
#endif
template<typename T> void print(T && x) {cout << x << "\n";}
template<typename T, typename... S> void print(T && x, S&&... y) {cout << x << ' ';print(y...);}

const int Mod=1000000007,Mod2=998244353;
const int MOD=Mod2;
const int maxn=200005;

//i_am_noob
//#define wiwihorz  
int n;
vector<int> vec[3];

int solve(int x, int y){
    int res=inf;
    for(auto i: vec[x]) if(sz(vec[y])&&i<=vec[y].back()) res=min(res,(*lower_bound(all(vec[y]),i))-i);
    for(auto i: vec[y]) if(sz(vec[x])&&i<=vec[x].back()) res=min(res,(*lower_bound(all(vec[x]),i))-i);
    return res;
}

void orzck(){
    cin >> n;
    rep(2*n){
        int x;
        char c;
        cin >> x >> c;
        if(c=='R') vec[0].pb(x);
        else if(c=='G') vec[1].pb(x);
        else vec[2].pb(x);
    }
    rep(3) sort(all(vec[i]));
    vector<int> vec1;
    rep(3) if(sz(vec[i])%2==0) vec1.pb(i);
    if(sz(vec1)==3){
        print(0);
        return;
    }
    int x,y,z;
    x=vec1[0];
    y=(x+1)%3;
    z=3-x-y;
    print(min(solve(y,z),solve(x,y)+solve(x,z)));
}

signed main(){
    ios_base::sync_with_stdio(0),cin.tie(0);
    #ifdef i_am_noob
    freopen("input1.txt","r",stdin);
    freopen("output1.txt","w",stdout);
    freopen("output2.txt","w",stderr);
    #endif
    cout << fixed << setprecision(15);
    int t;
    #ifdef wiwihorz
    cin >> t;
    #else
    t=1;
    #endif
    while(t--) orzck();
    return 0;
}