#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <numeric>
#include <functional>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define exrep(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;

struct Edge {
    ll to, cost;
    Edge(ll to, ll cost): to(to), cost(cost) {}
};
  
int main() {
    ll n, m;
    cin >> n >> m;

    vl w(n);
    ll maW = 0;
    rep(i, n) {
        cin >> w[i];
        chmax(maW, w[i]);
    }

    vl L(m), v(m);
    rep(i, m) {
        cin >> L[i] >> v[i];
        if(maW > v[i]) {
            out(-1);
            re0;
        }
    }

    vl dist(1<<n);  // dist[S] : ラクダi,j(i < j)間のラクダの集合がSのときのx[j] - x[i]の最小値
    rep(S, 1<<n) {
        ll W = 0;
        rep(i, n) {
            if(S & 1<<i) {
                W += w[i];
            }
        }
        rep(i, m) {
            if(W > v[i]) {
                chmax(dist[S], L[i]);
            }
        }
    }

    vl perm(n);
    rep(i, n) {
        perm[i] = i;
    }

    ll ans = -INF;
    do {
        vector<vector<Edge>> G(n);
        vl indeg(n);  // indeg[v] : 頂点vの入次数
        exrep(i, 0, n-2) {
            ll a = perm[i];
            ll S = 1<<a;
            exrep(j, i+1, n-1) {
                ll b = perm[j];
                S |= 1<<b;
                G[b].emplace_back(a, -dist[S]);
                indeg[a]++;
            }
        }

        ll sv = perm[n-1];
        ll gv = perm[0];

        queue<ll> Q;
        Q.push(sv);

        vl dp(n, INF);  // dp[v] : 頂点svからvへの最短距離
        dp[sv] = 0;
        while(!Q.empty()) {
            ll v = Q.front();  Q.pop();
            for(Edge e : G[v]) {
                ll u = e.to;
                indeg[u]--;
                chmin(dp[u], dp[v] + e.cost);
                if(indeg[u] == 0) {
                    Q.push(u);
                }
            }
        }

        chmax(ans, dp[gv]);
    } while(next_permutation(all(perm)));

    ans *= -1;

    out(ans);
    re0;
}
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define ROF(i, a, b) for (int i = a; i >= b; --i)
#define NEXT(i, x) for (int i = h[x]; i; i = e[i].u)
const int INF = 2e9;

inline int Max(const int &a, const int &b) { return a > b ? a : b; }
inline ll Min(const ll &a, const ll &b) { return a > b ? b : a; }

inline void read(int &x) {
    x = 0;
    int f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    x *= f;
}

const int N = 2e5 + 7;
int n;
ll s[3][3][3][3];
int sum[3];
ll ans;
bool ff[3];
int pos[3][3];
struct jgt {
    ll x;
    int id, f;
    bool operator<(const jgt &a) const { return x < a.x; }
} a[3][N], tmp[N];

int main() {
    read(n);
    n <<= 1;
    FOR(i, 1, n) {
        ll x;
        char ch;
        scanf(" %lld %c", &x, &ch);
        a[0][i].f = a[1][i].f = a[2][i].f = -1;
        if (ch == 'R') {
            ++sum[0];
            a[0][i] = (jgt){x, i, 0};
        } else if (ch == 'G') {
            ++sum[1];
            a[1][i] = (jgt){x, i, 1};
        } else if (ch == 'B') {
            ++sum[2];
            a[2][i] = (jgt){x, i, 2};
        }
    }
    FOR(i, 0, 2) {
        if (sum[i] & 1) ff[i] = 1;
    }
    if (ff[0] == ff[1] && ff[1] == ff[2] && ff[0] == 0) {
        puts("0");
        return 0;
    }
    FOR(i, 0, 1) {
        FOR(j, i + 1, 2) {
            FOR(k, 1, n) {
                if (a[i][k].f == i)
                    tmp[k] = a[i][k];
                else if (a[j][k].f == j)
                    tmp[k] = a[j][k];
                else {
                    tmp[k].x = (ll)1e18;
                    tmp[k].f = -1;
                }
            }
            std::sort(tmp + 1, tmp + n + 1);
            ll mn = (ll)1e18;
            int P = 0;
            FOR(k, 1, n - 1) {
                if (tmp[k].f != tmp[k + 1].f) {
                    if (tmp[k + 1].x - tmp[k].x < mn) {
                        mn = tmp[k + 1].x - tmp[k].x;
                        P = (tmp[k].f == i ? tmp[k].id : tmp[k + 1].id);
                    }
                }
            }
            s[i][j][0][0] = s[j][i][0][0] = mn;
            pos[i][j] = pos[j][i] = P;
        }
    }
    FOR(i, 0, 2) {
        FOR(j, 0, 2) {
            if (i == j) continue;
            FOR(w1, 0, 2) {
                FOR(w2, 0, 2) {
                    if (w1 == w2) continue;
                    if (w1 == i && w2 == j) continue;
                    if (w1 == j && w2 == i) continue;
                    FOR(k, 1, n) {
                        if (a[i][k].f == i)
                            tmp[k] = a[i][k];
                        else if (a[j][k].f == j)
                            tmp[k] = a[j][k];
                        else {
                            tmp[k].x = (ll)1e18;
                            tmp[k].f = -1;
                        }
                    }
                    tmp[pos[w1][w2]].x = (ll)1e18;
                    tmp[pos[w1][w2]].f = -1;
                    std::sort(tmp + 1, tmp + n + 1);
                    ll mn = (ll)1e18;
                    FOR(k, 1, n - 1) {
                        if (tmp[k].f != tmp[k + 1].f) {
                            mn = Min(mn, tmp[k + 1].x - tmp[k].x);
                        }
                    }
                    s[i][j][w1][w2] = mn;
                }
            }
        }
    }
    int k = 0, kk = 0, kkk = 0;
    if (ff[0] == 0)
        k = 1, kk = 2, kkk = 0;
    else if (ff[1] == 0)
        k = 0, kk = 2, kkk = 1;
    else if (ff[2] == 0)
        k = 0, kk = 1, kkk = 2;
    ans = s[k][kk][0][0];
    ans =
        Min(ans, Min(s[k][kkk][0][0] + s[kkk][kk][k][kkk], s[kkk][k][kk][kkk] + s[kk][kkk][0][0]));
    printf("%lld\n", ans);
    return 0;
}
