#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define vec vector
using namespace std;

const ll INF = 1e18;

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	#ifdef LOCAL
//		freopen("in1.txt", "r", stdin);
//		freopen("in1.txt", "w", stdout);
	#endif
	int n;
	cin >> n;
	vec<ll> a[3];
	int cnt[3] = {0, 0, 0};
	char dummy_stupid;
	for (ll i=0, x, y; i<2*n; i++) {
		cin >> x >> dummy_stupid;
		if (dummy_stupid == 'R') y = 0;
		if (dummy_stupid == 'G') y = 1;
		if (dummy_stupid == 'B') y = 2;
		a[y].push_back(x);
		cnt[y]++;
	}
	if ((cnt[0]&1)==0 && (cnt[1]&1)==0 && (cnt[2]&1)==0) {
		cout << 0 << '\n';
		return 0;
	}
	int p = -1, q = -1;
	for (int i=0; i<3; i++) {
		sort(a[i].begin(), a[i].end());
		if (cnt[i]&1) {
			q = p;
			p = i;
		}
	}
	assert(p!=-1 && q!=-1);
	int r = (-p-q+3) % 3;
	assert(p!=r && q!=r);
	
	ll res = INF;
	for (int i=0; i<a[p].size(); i++) {
		int pos = lower_bound(a[q].begin(), a[q].end(), a[p][i]) - a[q].begin();
		if (pos<a[q].size()) res = min(res, a[q][pos] - a[p][i]);
		if (pos>0) res = min(res, a[p][i] - a[q][pos-1]);
	}
	if (cnt[r] > 0) {
		vec<ll> sf(cnt[r]+1, INF);
		for (int i=0; i<cnt[r]; i++) {
			int pos = lower_bound(a[p].begin(), a[p].end(), a[r][i]) - a[p].begin();
			if (pos<cnt[p]) sf[i] = min(sf[i], a[p][pos] - a[r][i]);
			if (pos>0) sf[i] = min(sf[i], a[r][i] - a[p][pos-1]);
		}
		for (int i=cnt[r]-1; i>=0; i--) sf[i] = min(sf[i], sf[i+1]);
		ll pf = INF;
		for (int i=0; i<cnt[r]; i++) {
			ll cur = INF;
			int pos = lower_bound(a[q].begin(), a[q].end(), a[r][i]) - a[q].begin();
			if (pos<cnt[q]) cur = min(cur, a[q][pos] - a[r][i]);
			if (pos>0) cur = min(cur, a[r][i] - a[q][pos-1]);
			
			res = min(res, cur + min(pf, sf[i+1]));
			
			pos = lower_bound(a[p].begin(), a[p].end(), a[r][i]) - a[p].begin();
			if (pos<cnt[p]) pf = min(pf, a[p][pos] - a[r][i]);
			if (pos>0) pf = min(pf, a[r][i] - a[p][pos-1]);
		}
	}
	cout << res << '\n';
}
#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define rrep(i,n) for(int i=int(n-1);i>=int(0);--i)
#define fore(i,a) for(auto &i:a)
#define all(x) x.begin(),x.end() 
#define sz(x) ((int)(x).size())
#define bp(x) (__builtin_popcount((long long)(x)))
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define V vector
#define P pair<int,int>
#define TP tuple<int,int,int>
#define F first
#define S second
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
using ll = long long;
using ld = long double;
using namespace std;
const int INF = 1001001001;
const ll INFL = 1001001001001001001;
const int MAX = 2e6+1;

int main() {
    int n;cin>>n;n*=2;
    V<ll> r,g,b;
    rep(i,n){
        ll a;char c;
        cin>>a>>c;
        switch (c)
        {
        case 'R':
            r.push_back(a);
            break;
        case 'G':
            g.push_back(a);
            break;
        case 'B':
            b.push_back(a);
            break;
        }
    }

    if(sz(r)%2==0&&sz(g)%2==0&&sz(b)%2==0){
        cout<<0<<endl;
        return 0;
    }
    if(sz(r)%2==0)swap(g,r);
    else if(sz(b)%2==0)swap(g,b);
    sort(all(r));
    sort(all(g));
    sort(all(b));

    ll ans=INFL;
    rep(i,sz(r)){
        auto x = lower_bound(all(b),r[i]);
        if(x!=b.end()){
            chmin(ans,abs(r[i]-*x));
        }
        if(x!=b.begin()){
            x--;
            chmin(ans,abs(r[i]-*x));
        }
    }

    V<pair<ll,int>> minr,minb;
    rep(i,sz(g)){
        auto x = lower_bound(all(r),g[i]);
        if(x!=r.end()){
            minr.emplace_back(abs(g[i]-*x),i);
        }
        if(x!=r.begin()){
            x--;
            minr.emplace_back(abs(g[i]-*x),i);
        }
    }
    rep(i,sz(g)){
        auto x = lower_bound(all(b),g[i]);
        if(x!=b.end()){
            minb.emplace_back(abs(g[i]-*x),i);
        }
        if(x!=b.begin()){
            x--;
            minb.emplace_back(abs(g[i]-*x),i);
        }
    }
    sort(all(minr));
    sort(all(minb));
    rep(i,min(2,sz(minr))){
        rep(j,min(2,sz(minb))){
            if(minr[i].second==minb[j].second)continue;
            chmin(ans,minr[i].first+minb[j].first);
        }
    }

    cout<<ans<<endl;
    return 0;
}