// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h> 

using namespace std;
using i128 = __int128_t;
using u128 = __uint128_t;
using u64 = uint64_t;

//define
#define int long long
#define ll int
#define trav(i,v)   for(auto i: v)
#define rep(i,n)    for(int i=0;i<n;i++)
#define repu(i,k,n) for(int i=k;i<=n;i++)
#define repd(i,k,n) for(int i=k;i>=n;i--)
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define all(v)  v.begin(), v.end()
#define itn int 
#define sz(x) (int)x.size()
    
//typedef
typedef unsigned long long ull;
typedef long double ld;
typedef std::pair<int, int> pii;
typedef std::vector<int> vi;
typedef std::vector< pii > vii;
typedef std::vector< std::vector<int> > vvi;
typedef std::vector< std::pair < pii, int > > vpp;
 
const long long MOD = 1000000007;
// const long long MOD = 998244353;
const long double PI = 3.141592653589793238;
const long long pi = 31415926;
const long long inf = 1000000000000000000;
const long long small_inf = INT_MAX;

int modpow(int x, int n, int mod = MOD){ int res=1; while(n>0){ if(n&1) res=res*x%mod; x=x*x%MOD; n>>=1;} return res;}
int power(int x, int n){ int res=1; while(n>0){ if(n&1) res=res*x; x=x*x; n>>=1; } return res;}

void init(){
}

int find(int a, int b, vi *v){
    int n1 = sz(v[a]), n2 = sz(v[b]);
    int x = 0;
    int mn = inf;
    repu(y,0,n2-1){
        while(x<n1-1 && v[a][x]<v[b][y])
            x++;
        mn = min(mn,abs(v[a][x]-v[b][y]));
        if(x)
            mn = min(mn,abs(v[a][x-1]-v[b][y]));
    }
    return mn;
}

void solve()
{
/*Don't hurry, nothing good comes if you rush*/
    int n;
    cin>>n;
    vi cnt(3,0);
    map<char, int> mm;
    mm['R'] = 0;
    mm['G'] = 1;
    mm['B'] = 2;
    vector<char> c(2*n+1,0);
    vi v[3];
    repu(i,1,2*n){
        int tmp;
        cin>>tmp>>c[i];
        cnt[mm[c[i]]]++;
        v[mm[c[i]]].pb(tmp);
    }
    int odd = 0, f=-1, a=-1, b=-1;
    rep(i,3){
        sort(all(v[i]));
        if(cnt[i]%2){
            if(a==-1)
                a=i;
            else
                b=i;
            odd++;
        }
        else
            f = i;
    }
    if(odd==0){
        cout<<0<<endl;
        return;
    }
    if(cnt[f]==0){
        cout<<find(a,b,v)<<endl;
        return;
    }
    cout<<min(find(a,f,v)+find(b,f,v), find(a,b,v))<<endl;
}

signed main(){
    // #ifndef ONLINE_JUDGE
    //    freopen("./input.txt", "r", stdin);
    //    freopen("./output.txt", "w", stdout);
    // #endif
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    int t23 = 1, tt23 = 1; 
    // cin>>t23;
    init();
    while(tt23<=t23)
    {
        // cout<<"Case #"<<tt23<<": ";
        solve();
        tt23++;
    }
    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
}   
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll LINF = 1001001001001001001;
#ifdef _DEBUG
#define show(x)          \
	cerr << #x << " : "; \
	showVal(x)
template <typename T>
void showVal(const T &a) { cerr << a << endl; }
template <typename T>
void showVal(const vector<T> &a) {
	for (const T &v : a) cerr << v << " ";
	cerr << endl;
}
template <typename T>
void showVal(const vector<vector<T>> &a) {
	cerr << endl;
	for (const vector<T> &v : a) showVal(v);
}
#else
#define show(x)
#endif
int main() {
	int n;
	cin >> n;
	n *= 2;
	vector<ll> a(n);
	vector<string> c(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i] >> c[i];
	}
	vector<vector<ll>> v(3);
	for (int i = 0; i < n; i++) {
		int p;
		if (c[i] == "R") {
			p = 0;
		} else if (c[i] == "G") {
			p = 1;
		} else {
			p = 2;
		}
		v[p].push_back(a[i]);
	}
	vector<int> t;
	for (int i = 0; i < 3; i++) {
		if (v[i].size() & 1) {
			t.push_back(i);
		}
	}
	ll ans = LINF;
	if (t.size() == 2) {
		sort(v[t[1]].begin(), v[t[1]].end());
		for (int i = 0; i < v[t[0]].size(); i++) {
			int l = -1, r = v[t[1]].size(), m;
			while (abs(l - r) > 1) {
				m = (l + r) / 2;
				if (v[t[1]][m] <= v[t[0]][i]) {
					l = m;
				} else {
					r = m;
				}
			}
			if (l >= 0) ans = min(ans, abs(v[t[1]][l] - v[t[0]][i]));
			if (l + 1 < v[t[1]].size()) ans = min(ans, abs(v[t[1]][l + 1] - v[t[0]][i]));
		}
		t.push_back(3 - t[0] - t[1]);
		if (v[t[2]].size() != 0) {
			sort(v[t[2]].begin(), v[t[2]].end());
			ll tans = 0;
			for (int d = 0; d < 2; d++) {
				ll tvans = LINF;
				for (int i = 0; i < v[t[d]].size(); i++) {
					int l = -1, r = v[t[2]].size(), m;
					while (abs(l - r) > 1) {
						m = (l + r) / 2;
						if (v[t[2]][m] <= v[t[d]][i]) {
							l = m;
						} else {
							r = m;
						}
					}
					if (l >= 0) tvans = min(tvans, abs(v[t[2]][l] - v[t[d]][i]));
					if (l + 1 < v[t[2]].size()) tvans = min(tvans, abs(v[t[2]][l + 1] - v[t[d]][i]));
				}
				tans += tvans;
			}
			ans = min(ans, tans);
		}
	} else {
		ans = 0;
	}
	cout << ans << endl;
	return 0;
}