#include <iostream>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);

	int n; cin >> n;
	vector<ll> r, g, b;
	for (int i = 0; i < 2 * n; i++) {
		ll a; char c; cin >> a >> c;
		if (c == 'R') { r.push_back(a); }
		else if (c == 'G') { g.push_back(a); }
		else { b.push_back(a); }
	}

	vector<ll> v1, v2, v3;
	if (r.size() % 2 && g.size() % 2) { v1 = r; v2 = g; v3 = b; }
	else if (r.size() % 2 && b.size() % 2) { v1 = r; v2 = b; v3 = g; }
	else if (g.size() % 2 && b.size() % 2) { v1 = g; v2 = b; v3 = r; }
	else { cout << 0 << '\n'; return 0; }

	sort(v1.begin(), v1.end());
	sort(v2.begin(), v2.end());
	sort(v3.begin(), v3.end());

	ll ans = 1e16;
	ll min12 = 1e16, min13 = 1e16, min23 = 1e16;

	for (int i = 0; i < v1.size(); i++) {
		int cur = lower_bound(v2.begin(), v2.end(), v1[i]) - v2.begin();
		if (cur < v2.size()) { min12 = min(min12, v2[cur] - v1[i]); }
		if (cur > 0) { min12 = min(min12, v1[i] - v2[cur - 1]); }
	}
	for (int i = 0; i < v1.size(); i++) {
		int cur = lower_bound(v3.begin(), v3.end(), v1[i]) - v3.begin();
		if (cur < v3.size()) { min13 = min(min13, v3[cur] - v1[i]); }
		if (cur > 0) { min13 = min(min13, v1[i] - v3[cur - 1]); }
	}
	for (int i = 0; i < v2.size(); i++) {
		int cur = lower_bound(v3.begin(), v3.end(), v2[i]) - v3.begin();
		if (cur < v3.size()) { min23 = min(min23, v3[cur] - v2[i]); }
		if (cur > 0) { min23 = min(min23, v2[i] - v3[cur - 1]); }
	}

	ans = min(min12, min13 + min23);
	cout << ans << '\n';
}

#include <bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rep3(i,m,n) for(int (i)=m;(i)<=(n);(i)++)
#define rep3rev(i,m,n) for(int (i)=m;(i)>=(n);(i)--)
#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back

using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
using LD = long double;

template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }
template <typename T> void coutall(T v) { if(v.empty()){cout << endl; return;} for(auto i = v.begin(); i != --v.end(); i++){cout << *i << " ";} cout << *--v.end() << endl; }
template <typename T> void DEBUGall(T v) { if(v.empty()){cerr << endl; return;} for(auto i = v.begin(); i != --v.end(); i++){cerr << *i << " ";} cerr << *--v.end() << endl; }
inline void IN(void){ return; }
template <typename First, typename... Rest> void IN(First& first, Rest&... rest){ cin >> first; IN(rest...); return; }
inline void OUT(void){ cout << "\n"; return; }
template <typename First, typename... Rest> void OUT(First first, Rest... rest){ cout << first << " "; OUT(rest...); return; }
inline void DEBUG(void){ cerr << "\n"; return; }
template <typename First, typename... Rest> void DEBUG(First first, Rest... rest){ cerr << first << " "; DEBUG(rest...); return; }

void yes(bool ok = true){ cout << (ok ? "yes" : "no") << endl; }
void Yes(bool ok = true){ cout << (ok ? "Yes" : "No") << endl; }
void YES(bool ok = true){ cout << (ok ? "YES" : "NO") << endl; }
ll myceil(ll a, ll b) { return a >= 0 ? (a+b-1)/b : -((-a)/b); }
ll myfloor(ll a, ll b) { return a >= 0 ? a/b : -myceil(-a, b); }

const ll INF = 1002003004005006001;
void Main(){
    int n; IN(n);
    vector<vll> v(3);
    rep(i, n*2) {
        ll a; char c;
        IN(a, c);
        if(c == 'R') v[0].pb(a);
        if(c == 'G') v[1].pb(a);
        if(c == 'B') v[2].pb(a);
    }
    
    if(v[0].size() % 2 == 0 && v[1].size() % 2 == 0 & v[2].size() % 2 == 0){
        OUT(0);
        return;
    } 
    
    if(v[1].size() % 2 == 0) swap(v[0], v[1]);
    else if(v[2].size() % 2 == 0) swap(v[0], v[2]);
    rep(i, 3) sort(all(v[i]));
    /*
    DEBUGall(v[0]);
    DEBUGall(v[1]);
    DEBUGall(v[2]);
    */
    
    // v[0]: even, v[1], v[2]: odd
    
    auto f = [&](vll a, vll b){
        ll res = INF, ans = INF;
        for(auto x : a){
            auto it = upper_bound(all(b), x);
            if(it != b.end()){
                if(chmin(res, abs(x-*it))) ans = x;
            }
            if(it != b.begin()){
                it--;
                if(chmin(res, abs(x-*it))) ans = x;
            }
        }
        return Pll{res, ans};
    };
    
    auto [r1, a1] = f(v[1], v[2]);
    auto [r2, a2] = f(v[0], v[1]);
    auto [r3, a3] = f(v[0], v[2]);
    
    /*
    DEBUG(r1, a1);
    DEBUG(r2, a2);
    DEBUG(r3, a3);
    */
    
    if(a2 != a3 || a2 == INF){
        OUT(min(r1, r2 + r3));
        return;
    }
    
    vll w = v[0];
    w.erase(find(all(w), a2));
    auto [r4, a4] = f(w, v[1]);
    auto [r5, a5] = f(w, v[2]);
    
    ll ANS = min(r3 + r4, r2 + r5);
    OUT(min(r1, ANS));
    return;
}

int main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    Main();
    return 0;
}