#include <bits/stdc++.h>

using namespace std;

#define flush cout.flush

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pl = pair<ll, ll>;
const ll INF = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ld eps = 1e-9;
const ld PI = acos(-1);

ll cur = -1;

ll calc(vector<ll> &a, vector<ll> &b) {
    ll res = INF * INF;
    vector<pl> z;
    for (ll i = 0; i < a.size(); ++i) {
        if (a[i] == cur) {
            cur = -1;
            continue;
        }
        z.push_back({a[i], 0});
    }
    for (ll i = 0; i < b.size(); ++i) {
        z.push_back({b[i], 1});
    }
    ll last0 = -1, last1 = -1;
    sort(z.begin(), z.end());
    for (ll i = 0; i < z.size(); ++i) {
        if (z[i].second == 0) {
            if (last1 != -1 && abs(z[i].first - last1) < res) {
                res = min(res, abs(z[i].first - last1));
            }
            last0 = z[i].first;
        } else {
            if (last0 != -1) {
                res = min(res, abs(z[i].first - last0));
            }
            last1 = z[i].first;
        }
    }
    cur = last0;
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    ll n;
    cin >> n;
    vector<ll> r, g, b;

    for (ll i = 0; i < 2 * n; ++i) {
        ll a;
        cin >> a;
        char c;
        cin >> c;
        if (c == 'R')r.push_back(a);
        if (c == 'B')b.push_back(a);
        if (c == 'G')g.push_back(a);
    }
    if (r.size() % 2 == 0 && b.size() % 2 == 0) {
        cout << "0\n";
        return 0;
    }
    ll res;
    if (r.size() % 2 == 0) {
        res = calc(g, b);
        ll tmp = calc(r, g);
        res = min(res, tmp + calc(r, b));
        cur = -1;
        tmp = calc(r, b);
        res = min(res, tmp + calc(r, g));
        cur = -1;
    } else if (b.size() % 2 == 0) {
        res = calc(r, g);
        ll tmp = calc(b, g);
        res = min(res, tmp + calc(b, r));
        cur = -1;
        tmp = calc(b, r);
        res = min(res, tmp + calc(b, g));
        cur = -1;
    } else {
        res = calc(r, b);
        ll tmp = calc(g, r);
        res = min(res, tmp + calc(g, b));
        cur = -1;
        tmp = calc(g, b);
        res = min(res, tmp + calc(g, r));
        cur = -1;
    }
    cout << res << "\n";
    return 0;
}
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

typedef long long ll;

int main() {
    int N;
    scanf("%d", &N);
    vector<vector<ll>> b(3, vector<ll>(0));
    ll a;
    char c;
    rep(i, 2 * N) {
        cin >> a >> c;
        if (c == 'R') b[0].push_back(a);
        if (c == 'G') b[1].push_back(a);
        if (c == 'B') b[2].push_back(a);
    }
    rep(i, 3) sort(b[i].begin(), b[i].end());
    vector<ll> d(3, 1e18);
    rep(i, 3) {
        rep(j, b[i].size()) {
            int k = lower_bound(b[(i + 1) % 3].begin(), b[(i + 1) % 3].end(), b[i][j]) - b[(i + 1) % 3].begin();
            if (k > 0) d[i] = min(d[i], b[i][j] - b[(i + 1) % 3][k - 1]);
            if (k < b[(i + 1) % 3].size()) d[i] = min(d[i], b[(i + 1) % 3][k] - b[i][j]);
        }
    }
    //rep(i, 3) cout << d[i] << endl;
    if ((b[0].size() % 2 == 0) && (b[1].size() % 2 == 0) && (b[2].size() % 2 == 0)) {
        printf("0\n");
        return 0;
    }
    if (b[0].size() % 2 == 0) {
        printf("%lld\n", min(d[1], d[0] + d[2]));
    }
    if (b[1].size() % 2 == 0) {
        printf("%lld\n", min(d[2], d[0] + d[1]));
    }
    if (b[2].size() % 2 == 0) {
        printf("%lld\n", min(d[0], d[2] + d[1]));
    }
}