//in dp prefix suffix sum helps..
#include<iostream>
#include<vector>
#include<string.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<stack>
#include <chrono>
#include <random>
#include <iterator>
#include <map>
#include<list>
#include <algorithm>
#include <fstream>
#include<unordered_map>
#include<set>
#include<queue>
#define int  long long
#define double long double
#define pb   push_back
#define mp   make_pair
#define pii  pair<int,int>
#define pip pair<int,pii>
#define vip vector<pii>
#define vi vector<int>
#define vs vector<string>
#define vd vector<double>
#define mii  map<pii, int>
#define qu queue<pii>
#define set set<int>
#define multiset multiset<int>
#define rep(i, a, b) for(int i=a;i<b;i++)
#define rep1(i, a, b) for(int i=b;i>=a;i--)
#define viv vector<pip>
#define stack stack<int>
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define it iterator
#define pq priority_queue<int>
#define pq1 priority_queue <pii, vector<pii>, greater<pii> >
#define mem(x, y) memset(x, (int)y, sizeof(x))
#define ps(x,y) fixed<<setprecision(y)<<x
// author :: Anurag Anand.
using namespace std;
int z=  1e9+7;
//int z= 998244353;
int gcd(int a, int b){
    if(a==0)return b;
    if(b==0)return a;
    return gcd(b, a%b);
}
int power(int a,int b)
{
	int res=1;
	while(b)
	{
		if(b&1)
		{
			res=(res*a)%z;
			b--;
		}
		a=(a*a)%z;
		b=b>>1;
	}
	return res;
}
vi sv;
int fact[200001];
int ncr(int n, int r){
	int p=fact[n];
	p*=power(fact[r], z-2);
	p%=z;
	p*=power(fact[n-r], z-2);
	p%=z;
	return p;
}
// vi seg;
// void arrange(int i, int v, int x, int lx, int rx){
// 	if(rx-lx==1){
// 		seg[x]=v;
// 		return;
// 	}
// 	int mid=(lx+rx)/2;
// 	if(i<mid) arrange(i, v, 2*x+1, lx, mid);
// 	else arrange(i, v, 2*x+2, mid, rx);
// 	seg[x]= min(seg[2*x+1], seg[2*x+2]);
// }
// int calc(int l, int r, int x, int lx, int rx){
// 	if(lx>=r||l>=rx) return 1e9;
// 	if(lx>=l&&rx<=r) return seg[x];
// 	int mid=(lx+rx)/2;
// 	int k1=calc(l, r, 2*x+1, lx, mid), k2=calc(l, r, 2*x+2, mid, rx);
// 	return min(k1, k2);
// }
vi pp;
int32_t main()
{
    ios_base::sync_with_stdio(false);
	cin.tie(NULL);
    cout.tie(NULL);
	pp.assign(1000001, 0);
	for(int i=2;i<1000;i++){
		if(!pp[i]){
			for(int j=i*i;j<=1000000;j+=i){
				pp[j]=1;
			}
		}
	}
	fact[0]=1;
	rep(i,1,200001) fact[i]=i*fact[i-1], fact[i]%=z;
	for(int i=2;i<=1000000;i++) if(!pp[i]) sv.pb(i);
	int t;
	t=1;
	//cin>>t;
	while(t--){
		int n;
		cin>>n;
		multiset sr, sb, sg;
		rep(i,0,2*n){
			int x;
			char c;
			cin>>x>>c;
			if(c=='R') sr.insert(x);
			else if(c=='G') sg.insert(x);
			else sb.insert(x);
		}
		if(sr.size()%2==0&&sb.size()%2==0&&sg.size()%2==0) cout<<"0\n";
		else{
			multiset s1, s2, s3;
			if(sr.size()&1&&sb.size()&1){
				s1=sr;
				s2=sb;
				s3=sg;
			}
			else if(sr.size()&1&&sg.size()&1){
				s1=sr;
				s2=sg;
				s3=sb;
			}
			else{
				s1=sg;
				s2=sb;
				s3=sr;
			}
			int a=1e15, b1=1e15, b2=1e15;
			multiset :: it it;
			for(it=s1.begin();it!=s1.end();it++){
				multiset :: it it1= s2.upper_bound(*it);
				int k;
				if(it1==s2.end()){
					k=*s2.rbegin();
					a=min(a, abs(k-*it));
				}
				else{
					k=*it1;
					a=min(a, abs(k-*it));
					if(it1!=s2.begin()){
						it1--;
						k=*it1;
						a=min(a, abs(k-*it));
					}
				}
				if(s3.size()){
					it1=s3.upper_bound(*it);
				if(it1==s3.end()){
					k=*s3.rbegin();
					b1=min(b1, abs(k-*it));
				}
				else{
					k=*it1;
					b1= min(b1, abs(k-*it));
					if(it1!=s3.begin()){
						it1--;
						k=*it1;
						b1=min(b1, abs(k-*it));
					}
				}
				}
			}
			if(s3.size()){
				for(it=s2.begin();it!=s2.end();it++){
				multiset :: it it1= s3.upper_bound(*it);
				int k;
				if(it1==s3.end()){
					k=*s3.rbegin();
					b2=min(b2, abs(k-*it));
				}
				else{
					k=*it1;
					b2= min(b2, abs(k-*it));
					if(it1!=s3.begin()){
						it1--;
						k=*it1;
						b2=min(b2, abs(k-*it));
					}
				}
			}
			}
			cout<<min(a, b1+b2)<<"\n";
		}
	}
}
// *********************************************************************************
// *                        MURTAZA MUSTAFA KHUMUSI                                *
// *                           CSE - 2019-2023                                     *
// *********************************************************************************

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define loop(i, a, n) for (int i = a; i < n; i++)
#define loope(i, b, n) for (int i = b; i <= n; i++)
#define loopit(a) for (auto it = a.begin(); it != a.end(); it++)
#define ms(a, b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define pi pair<int, int>
#define ff first
#define ss second
#define bloop(i, a, b) for (int i = a; i > b; i--)
#define bloope(i, a, b) for (int i = a; i >= b; i--)
#define PQ priority_queue<int> pq;
#define vi vector<int>
#define vii vector<vector<int>>
#define si set<int>
#define NO cout<<"NO\n";
#define YES cout<<"YES\n";
#define MPQ priority_queue<pi, vector<int>, greater<pi>> mpq;
#define io                        \
    ios_base::sync_with_stdio(0); \
    cin.tie(NULL);                \

void solve()
{
    int n; cin >> n;
    vector<ll>in[3];
    auto ret = [](char ch) {
        if (ch == 'R')return 0;
        else if (ch == 'G')return 1;
        else return 2;
    };

    loop(i, 0, n << 1) {
        ll num; cin >> num;
        char c; cin >> c;
        in[ret(c)].pb(num);
    }

    ll mx = 1e16;

    vi odd;
    loop(i, 0, 3) {
        if (in[i].size() & 1)odd.pb(i);
    }

    // dis(odd);

    ll ans = 0;
    if (odd.size() == 0) {
        cout << ans << "\n";
        return;
    }

    ans = mx;
    int od = odd[0];
    loop(i, 0, 3)sort(in[i].begin(), in[i].end());


    // at least tow odds guranteed
    int rem = 3 - (odd[0] + odd[1]);


    auto dif = [&](vector<ll> & vec, ll & v) {
        ll fin = mx;
        int val = lower_bound(vec.begin(), vec.end(), v) - vec.begin();
        if (val != vec.size())fin = min(fin, llabs(vec[val] - v));
        val--;
        if (val >= 0)fin = min(fin, llabs(vec[val] - v));

        return fin;
    };

    // direct difference between odd elems
    for (auto &v : in[odd[1]]) {
        ll ret = dif(in[odd[0]], v);
        // cout << odd[0] << " " << odd[1] << " " << ret << "\n";
        ans = min(ans, dif(in[odd[0]], v));
    }

    // use two intermediates
    int siz = in[rem].size();


    // cout << in[0].size() << " " << in[1].size() << " " << in[2].size() << " " << in[rem].size() << " " << ans << "\n";

    if (siz == 0) {
        cout << ans << "\n";
        return;
    }
    // at least two elements

    vector<ll>pref(siz), suf(siz), val(siz);
    ll fn = mx;
    // Itermediate difference
    loop(i, 0, siz) {
        val[i] = dif(in[odd[0]], in[rem][i]);
    }
    pref[0] = val[0];
    suf[siz - 1] = val[siz - 1];

    loop(i, 1, siz) {
        pref[i] = min(pref[i - 1], val[i]);
        suf[siz - i - 1] = min(suf[siz - i], val[siz - i - 1]);
    }

    loop(i, 0, siz) {
        ll prev = (i ? pref[i - 1] : mx), nex = (i + 1 < siz ? suf[i + 1] : mx);
        ll mn = min(prev, nex);
        ans = min(ans, mn + dif(in[odd[1]], in[rem][i]));
    }

    cout << ans << "\n";
}
int main()
{
    io;
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int test = 1, i = 0;
    // cin >> test;
    while (i++ != test)
    {
        // cout<<"Case #"<<i<<": ";
        solve();
    }
    return 0;
}
