#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
template<class T> using V = vector<T>;
using VI = V<int>;
using VL = V<ll>;
using VS = V<string>;
template<class T> using PQ = priority_queue<T, V<T>, greater<T>>;
using graph = V<VI>;
template<class T> using w_graph = V<V<pair<int, T>>>;
#define FOR(i,a,n) for(int i=(a);i<(n);++i)
#define eFOR(i,a,n) for(int i=(a);i<=(n);++i)
#define rFOR(i,a,n) for(int i=(n)-1;i>=(a);--i)
#define erFOR(i,a,n) for(int i=(n);i>=(a);--i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define out(y,x) ((y)<0||h<=(y)||(x)<0||w<=(x))
#ifdef _DEBUG
#define line cout << "-----------------------------\n"
#define stop system("pause")
#endif
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
constexpr ld eps = 1e-10;
template<class T>inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; }return false; }
template<class T>inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; }return false; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T>inline istream& operator>>(istream& is, V<T>& v) { for (auto& a : v)is >> a; return is; }
template<class T, class U>inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template<class T>inline V<T> vec(size_t a) { return V<T>(a); }
template<class T>inline V<T> defvec(T def, size_t a) { return V<T>(a, def); }
template<class T, class... Ts>inline auto vec(size_t a, Ts... ts) { return V<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); }
template<class T, class... Ts>inline auto defvec(T def, size_t a, Ts... ts) { return V<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); }
template<class T>inline void print(const T& a) { cout << a << "\n"; }
template<class T, class... Ts>inline void print(const T& a, const Ts&... ts) { cout << a << " "; print(ts...); }
template<class T>inline void print(const V<T>& v) { for (int i = 0; i < v.size(); ++i)cout << v[i] << (i == v.size() - 1 ? "\n" : " "); }
template<class T>inline void print(const V<V<T>>& v) { for (auto& a : v)print(a); }
template<class T>inline T sum(const V<T>& a, int l, int r) { return a[r] - (l == 0 ? 0 : a[l - 1]); }
template<class T>inline void END(T s) { print(s); exit(0); }
void END() { exit(0); }

int main() {
    init();

    int n; cin >> n;
    VL a(n * 2); cin >> a;

    vector<char> c(n * 2);
    vector<pair<int, int>> b(n * 2);
    FOR(i, 0, n * 2)b[i] = { a[i],i };
    sort(all(b));
    FOR(i, 0, n * 2) {
        if (i < n)c[b[i].second] = 'o';
        else c[b[i].second] = 'x';
    }

    string ans(n * 2, '?');
    stack<pair<int, char>> st;
    FOR(i, 0, n * 2) {
        if (st.empty()) {
            st.emplace(i, c[i]);
        }
        else {
            if (st.top().second == c[i]) {
                st.emplace(i, c[i]);
            }
            else {
                ans[st.top().first] = '(';
                ans[i] = ')';
                st.pop();
            }
        }
    }

    print(ans);

    return 0;
}
#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rrep1(i, n) for (int i = n; i >= 1; i--)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define eb emplace_back
#define fi first
#define se second
#define sz(x) (int)(x).size()
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
typedef long long int ll;
typedef pair<ll, ll> P;
// typedef modint1000000007 mint;

void speedUpIO() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
}

template <class T> bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T> bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}
/*--------------------------------------------------*/
const int INF = 1e9;
const ll LINF = 1e18;
const int MAX = 100010;

void solve() {
    ll n;
    cin >> n;
    V<ll> r, g, b;
    rep(i, 2 * n) {
        ll a;
        char c;
        cin >> a >> c;
        if (c == 'R') r.eb(a);
        if (c == 'G') g.eb(a);
        if (c == 'B') b.eb(a);
    }
    if (sz(r) % 2 == 0 && sz(g) % 2 == 0 && sz(b) % 2 == 0) {
        cout << 0 << "\n";
        return;
    }
    if (sz(r) % 2 == 0) swap(r, b);
    if (sz(g) % 2 == 0) swap(g, b);
    sort(all(r));
    sort(all(g));
    sort(all(b));
    ll ans = LINF;
    {
        ll m = sz(r);
        rep(i, m) {
            ll j = lower_bound(all(g), r[i]) - g.begin();
            if (j < sz(g)) chmin(ans, abs(g[j] - r[i]));
            if (j > 0) chmin(ans, abs(g[j - 1] - r[i]));
        }
    }
    if (sz(b) == 0) {
        cout << ans << "\n";
        return;
    }
    {
        V<P> rv, gv;
        rep(i, sz(r)) {
            V<P> rrv;
            ll j = lower_bound(all(b), r[i]) - b.begin();
            if (j < sz(b)) rrv.eb(abs(b[j] - r[i]), j);
            if (j + 1 < sz(b)) rrv.eb(abs(b[j + 1] - r[i]), j + 1);
            if (j - 1 >= 0) rrv.eb(abs(b[j - 1] - r[i]), j - 1);
            sort(all(rrv));
            set<ll> st;
            rep(k, sz(rrv)) {
                if (st.count(rrv[k].se) > 0) continue;
                st.insert(rrv[k].se);
                rv.eb(rrv[k]);
                if (sz(st) == 2) break;
            }
        }
        sort(all(rv));
        rep(i, sz(g)) {
            V<P> ggv;
            ll j = lower_bound(all(b), g[i]) - b.begin();
            if (j < sz(b)) ggv.eb(abs(b[j] - g[i]), j);
            if (j + 1 < sz(b)) ggv.eb(abs(b[j + 1] - g[i]), j + 1);
            if (j - 1 >= 0) ggv.eb(abs(b[j - 1] - g[i]), j - 1);
            sort(all(ggv));
            set<ll> st;
            rep(k, sz(ggv)) {
                if (st.count(ggv[k].se) > 0) continue;
                st.insert(ggv[k].se);
                gv.eb(ggv[k]);
                if (sz(st) == 2) break;
            }
        }
        sort(all(gv));
        rep(i, min(2, sz(rv))) rep(j, min(2, sz(gv))) {
            if (rv[i].se == gv[j].se) continue;
            ll t = rv[i].fi + gv[j].fi;
            chmin(ans, t);
        }
    }
    cout << ans << "\n";
}

int main() {
    speedUpIO();
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
        // cout << solve() << "\n";
        // cout << (solve() ? "Yes" : "No") << "\n";
        // cout << fixed << setprecision(15) << solve() << "\n";
    }
    return 0;
}
