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

#define F first
#define S second

int main(){
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
 	ll n; cin >> n;
 	vector<ll> r, g, b; 
 	for(int i = 0; i < 2*n; i++){
 		ll x; cin >> x;
 		char c; cin >> c;
 		if(c == 'R') r.push_back(x);
 		else if(c == 'G') g.push_back(x);
 		else b.push_back(x);
 	}   
 	sort(r.begin(), r.end());
 	sort(g.begin(), g.end());
 	sort(b.begin(), b.end());
 	ll x = 0, y = 0, z = 0, ans = LLONG_MAX;
 	x = y = z = ans;
 	if(r.size() & 1){
 		if(b.size() & 1) swap(g, b);
 	} else if(g.size() & 1){
 		swap(r, b);
 	} else {
 		ans = 0;
 	}
 	for(auto &i: r){
 		auto p = lower_bound(g.begin(), g.end(), i);
 		if(p != g.end()) x = min(x, abs(i - *p));
 		if(p != g.begin()) x = min(x, abs(i - *prev(p)));
 	}
 	for(auto &i: g){
 		auto p = lower_bound(b.begin(), b.end(), i);
 		if(p != b.end()) y = min(y, abs(i - *p));
 		if(p != b.begin()) y = min(y, abs(i - *prev(p)));
 	}
 	for(auto &i: r){
 		auto p = lower_bound(b.begin(), b.end(), i);
 		if(p != b.end()) z = min(z, abs(i - *p));
 		if(p != b.begin()) z = min(z, abs(i - *prev(p)));
 	}
 	ans = min(ans, x);
 	if(y != LLONG_MAX && z != LLONG_MAX) ans = min(ans, y + z);
 	cout << ans << '\n';
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> using vc = vector<T>;
template<class T> using vvc = vc<vc<T>>;
template<class T> using vvvc = vc<vvc<T>>;
template<class T> using vvvvc = vvc<vvc<T>>;
template<class T> using PQ = priority_queue<T>;
template<class T> using invPQ = priority_queue<T, vector<T>, greater<T>>;
using IP = pair<int, int>;
using LP = pair<ll, ll>;
#define all(x) begin(x), end(x)
#define rep(i, n) for (int i = 0; (i) < (int)(n); i++)
#define rep3(i, m, n) for (int i = (m); (i) < (int)(n); i++)
#define repr(i, n) for (int i = (n) - 1; (i) >= 0; i--)
#define rep3r(i, m, n) for (int i = (n) - 1; (i) >= (int)(m); i--)
template<class T> inline bool chmax(T & a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T & a, T b) { if (a > b) { a = b; return 1; } return 0; }
constexpr long long LINF = 4611686015206162431;

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int N;
    cin >> N;
    ll a[2*N];
    vc<int> c(2*N);
    vvc<ll> dogs(3);
    rep (i, N*2) {
        cin >> a[i];
        char col;
        cin >> col;
        if (col == 'G') c[i] = 1;
        else if (col == 'B') c[i] = 2;
        dogs[c[i]].emplace_back(a[i]);
    }
    rep (i, 3) {
        sort(all(dogs[i]));
    }

    int oddcnt = dogs[0].size() % 2 + dogs[1].size() % 2 + dogs[2].size() % 2;
    ll ans = LINF;
    if (oddcnt == 0) ans = 0;
    else {
        int even = 0;
        rep (i, 3) if (dogs[i].size() % 2 == 0) even = i;
        int odd1 = (even + 1) % 3, odd2 = (even + 2) % 3;

        rep (i, dogs[odd1].size()) {
            int pos = lower_bound(all(dogs[odd2]), dogs[odd1][i]) - dogs[odd2].begin();
            if (pos < dogs[odd2].size()) chmin(ans, abs(dogs[odd2][pos] - dogs[odd1][i]));
            if (pos > 0) {
                chmin(ans, abs(dogs[odd2][pos-1] - dogs[odd1][i]));
            }
        }

        ll tmp1 = LINF, tmp2 = LINF;
        rep (i, dogs[odd1].size()) {
            int pos1 = lower_bound(all(dogs[even]), dogs[odd1][i]) - dogs[even].begin();
            if (pos1 < dogs[even].size()) chmin(tmp1, abs(dogs[even][pos1] - dogs[odd1][i]));
            if (pos1 > 0) chmin(tmp1, abs(dogs[even][pos1-1] - dogs[odd1][i]));
        }
        rep (i, dogs[odd2].size()) {
            int pos2 = lower_bound(all(dogs[even]), dogs[odd2][i]) - dogs[even].begin();
            if (pos2 < dogs[even].size()) chmin(tmp2, abs(dogs[even][pos2] - dogs[odd2][i]));
            if (pos2 > 0) chmin(tmp2, abs(dogs[even][pos2-1] - dogs[odd2][i]));
        }
        chmin(ans, tmp1 + tmp2);
    }

    cout << ans << endl;
}