#include <bits/stdc++.h>

using namespace std;

#define x first
#define y second
#define mp make_pair
#define pb push_back

typedef long long ll;

const ll MOD = 1e9+7;
const ll INF = 1e18+5;

pair <ll, char> p[200005];
multiset <ll> r;
multiset <ll> g;
multiset <ll> b;

ll solve1(multiset <ll> a, multiset<ll> b) {
	ll sol = INF;
	for (ll x:a) {
		multiset <ll> :: iterator it = b.lower_bound(x);
		if (it != b.end()) {
			sol = min(sol, *it-x);
		}
		if (it != b.begin()) {
			it--;
			sol = min(sol, x-*it);
		}
	}
	return sol;
}

ll solve2(multiset <ll> a, multiset<ll> b, multiset <ll> s) {
	ll ba = INF;
	ll bb = INF;
	ll sol = INF;
	for (ll x:s) {
		ll A = INF;
		ll B = INF;
		multiset <ll> :: iterator it = a.lower_bound(x);
		if (it != a.end()) {
			A = min(A, *it-x);
		}
		if (it != a.begin()) {
			it--;
			A = min(A, x-*it);
		}
		it = b.lower_bound(x);
		if (it != b.end()) {
			B = min(B, *it-x);
		}
		if (it != b.begin()) {
			it--;
			B = min(B, x-*it);
		}
		sol = min(sol, A+bb);
		sol = min(sol, B+ba);
		ba = min(ba, A);
		bb = min(bb, B);
	}
	return sol;
}

int main() {
	int n;
	scanf("%d", &n);
	n *= 2;
	for (int i=0 ; i<n ; i++) {
		scanf("%lld %c", &p[i].x, &p[i].y);
		if (p[i].y == 'R') r.insert(p[i].x);
		if (p[i].y == 'G') g.insert(p[i].x);
		if (p[i].y == 'B') b.insert(p[i].x);
	}
	if (r.size()%2 == 0 and g.size()%2 == 0 and b.size()%2 == 0) {
		printf("0\n");
		return 0;
	}
	if (r.size()%2 == 0) printf("%lld\n", min(solve1(g, b), solve2(g, b, r)));
	if (g.size()%2 == 0) printf("%lld\n", min(solve1(r, b), solve2(r, b, g)));
	if (b.size()%2 == 0) printf("%lld\n", min(solve1(r, g), solve2(r, g, b)));
	return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define  pb push_back
#define inf 1e18
// 1000000007 , 998244353
// cout<<"? "<<endl for interactive

#define mod 998244353
#define  mp make_pair
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define clr(x,y,z) (x).clear(),(x).resize((y),(z))

typedef long long int lli;
typedef vector<lli> vli;
typedef vector<int> vi;
typedef tuple<int, int, long long int>tup;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<lli, lli> pll;
typedef pair<int, int> pii;

#define csb(x) __builtin_popcount(x)
#define clz(x) __builtin_clz(x)
#define ctz(x) __builtin_ctz(x)
#define PI 3.1415926535897932384626
#define deb(x) cout << #x << " = " << x << endl;
#define show(x) {for( auto i : x) cout << i << '\t'; cout << endl;}
#define show2(x,l1,l2) {for( int i=0;i<l1;i++){for(int j=0;j<l2;j++) cout << x[i][j] << '\t'; cout << endl;}}
#define rep(i,st,ed) for(int i=(st);i<(ed);i++)
#define rrep(i,ed,st) for(int i=ed;i>=st;i--)
#define ff first
#define ss second
#define minheap(type) priority_queue<type,vector<type>,greater<type>>

template <typename T> void ckmin(T &a, const T &b) { a = min(a, b); }
template <typename T> void ckmax(T &a, const T &b) { a = max(a, b); }

long long gcd(ull a, ull b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
void sieve(vector<int>&spf) {int n = spf.size(); rep(i, 2, n) {if (!spf[i]) {/*prime.pb(i)*/; for (int j = i; j < n; j += i) spf[j] = i; }}}
lli power(lli x, lli y, long long int m = mod) { lli ans = 1; x %= m; while (y) {if (y & 1)ans = (x * ans) % m; x = (x * x) % m; y >>= 1;} return ans;}
lli ncr(vli&fact, int n, int r) {return (n >= r) ? ((fact[n] * ((power(fact[r], mod - 2) * power(fact[n - r], mod - 2) ) % mod)) % mod) : 0;}

// int dx[] = {1, -1, 0, 0};
// int dy[] = {0, 0, 1, -1};


void solve() {
    int n;
    cin >> n;
    n*=2;
    vector<pll> arr(n);
    vi odd(3,0);
    rep(i,0,n){
        cin>>arr[i].ff;
        char c;
        cin>>c;
        if(c=='R')arr[i].ss = 1,odd[0]++;
        else if(c=='G')arr[i].ss = 2,odd[1]++;
        else arr[i].ss = 4,odd[2]++;
    }
    rep(i,0,3)odd[i]%=2;
    int state = (odd[0])^(odd[1]<<1)^(odd[2]<<2);
    sort(all(arr));
    vli scene(7,1e16);
    rep(i,0,n-1){
        if(arr[i].ss != arr[i+1].ss){
            scene[arr[i].ss ^ arr[i+1].ss] =min(scene[arr[i].ss ^ arr[i+1].ss],abs(arr[i].ff - arr[i+1].ff ));
        }
    }
    // deb(state)
    // show(scene)
    if(state == 0){
        cout<<0;
    }
    else if (state == 6){
        cout<< min ({scene[3]+scene[5],scene[6]});
    }
    else if(state == 3){
        cout<< min ({scene[6]+scene[5],scene[3]});

    }
    else{
        cout<< min ({scene[3]+scene[6],scene[5]});

    }


}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int z = 1;
    // cin >> z;
    while (z--) {
        solve();
    }
    return 0;

}