#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define Pr pair<ll,ll>
#define Tp tuple<ll,ll,ll>
using Graph = vector<vector<int>>;

ll mod = 1000000007;

int main() {
    ll N; cin >> N;
    vector<char> col = {'R','G','B'};
    vector<vector<ll>> cute(3);
    rep(i,2*N){
        ll a; char c; cin >> a >> c;
        rep(j,3){
            if(c==col[j]) cute[j].push_back(a);
        }
    }
    bool even[3];
    rep(i,3){
        cute[i].push_back(2e18);
        cute[i].push_back(-2e18);
        sort(cute[i].begin(),cute[i].end());
        if(cute[i].size()%2==0) even[i] = true;
        else even[i] = false;
    }
    if(even[0]&&even[1]&&even[2]){
        cout << 0 << endl; return 0;
    }
    ll md[3];
    rep(i,3) md[i] = 2e18;
    rep(i,3){
        rep(j,3){
            if(i<=j) continue;
            int k = 3-i-j;
            int M = cute[i].size()-2;
            for(int l=1;l<=M;l++){
                ll c = cute[i][l];
                int s = lower_bound(cute[j].begin(),cute[j].end(),c)-cute[j].begin();
                md[k] = min({md[k],abs(c-cute[j][s]),abs(c-cute[j][s-1])});
            }
        }
    }
    ll ans = 2e18;
    rep(i,3){
        if(even[i]) ans = min(ans,md[i]);
        //cout << md[i] << endl;
        rep(j,3){
            if(i<=j) continue;
            if(!even[i]&&!even[j]) ans = min(ans,md[i]+md[j]);
        }
    }
    cout << ans << endl;
}
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define deb(...)
#endif
#define endl "\n"
#define pb push_back
#define int long long
#define ll long long
#define pii pair<int,int>
#define F first
#define S second
#define all(c) c.begin(),c.end()
#define read(v) for(auto &it:v) cin>>it;
const int inf = 1e18;
const int N   = 2e5 + 5;
const int mod = 1000000007;
vector <int> adj[4];
int solve(vector <int> &B, vector <int> &C) {
    int ans = inf;
    if (B.empty() || C.empty()) {
        return ans;
    }
    for (auto it : B) {
        // find best match for this in C?
        auto x = lower_bound(all(C), it);
        if (x == C.end()) {
            ans = min(ans, abs(it - *C.rbegin()));
        }
        else {
            if (x == C.begin()) {
                ans = min(ans, abs(it - *C.begin()));
            }
            else {
                ans = min(ans, abs(it - *x));
                x--;
                ans = min(ans, abs(it - *x));
            }
        }
    }
    for (auto it : C) {
        // find best match for this in C?
        auto x = lower_bound(all(B), it);
        if (x == B.end()) {
            ans = min(ans, abs(it - *B.rbegin()));
        }
        else {
            if (x == B.begin()) {
                ans = min(ans, abs(it - *B.begin()));
            }
            else {
                ans = min(ans, abs(it - *x));
                x--;
                ans = min(ans, abs(it - *x));
            }
        }
    }
    return ans;

}
void test_case() {
    int n;
    cin >> n;
    for (int i = 0; i < 2 * n; i++) {
        int a;
        char b;
        cin >> a >> b;
        if (b == 'R') {
            adj[1].pb(a);

        } else if (b == 'G') {
            adj[2].pb(a);

        } else {
            adj[3].pb(a);
        }
    }
    sort(all(adj[1]));
    sort(all(adj[2]));
    sort(all(adj[3]));

    int r = adj[1].size(), g = adj[2].size(), b = adj[3].size();
    int cnt = 0, who = -1;
    for ( int val : {r, g, b}) {
        if (val % 2 == 1) {

            cnt++;
        }
        else {
            if (val == r) {
                who = 1;
            }
            else if (val == g) {
                who = 2;
            }
            else {
                who = 3;
            }
        }
    }
    if (cnt == 0) {
        cout << 0 << endl;
        return;
    }
    if (cnt == 1 || cnt == 3) {
        assert(false);
    }
    vector <int> A = adj[who];
    set <int> st = {1, 2, 3};
    st.erase(who);
    vector <int> B = adj[*st.begin()];
    vector <int> C = adj[*st.rbegin()];
    cout << min(solve(B, C), solve(A, C) + solve(A, B)) << endl;











}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int  t = 1;
    // cin >> t;
    while (t--)test_case();
    return 0;
}
