#line 1 "/workspaces/compro/lib/template.hpp"


#line 1 "/workspaces/compro/lib/io/vector.hpp"
#include <iostream>
#include <vector>

#ifndef IO_VECTOR
#define IO_VECTOR

template <class T> std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
  int size = v.size();
  for (int i = 0; i < size; i++) {
    std::cout << v[i];
    if (i != size - 1)
      std::cout << " ";
  }
  return out;
}

template <class T> std::istream &operator>>(std::istream &in, std::vector<T> &v) {
  for (auto &el : v) {
    std::cin >> el;
  }
  return in;
}

#endif
#line 4 "/workspaces/compro/lib/template.hpp"
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
#define MM << " " <<

using namespace std;

template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

// 重複を消す。計算量はO(NlogN)
template <class T> void unique(std::vector<T> &v) {
  std::sort(v.begin(), v.end());
  v.erase(std::unique(v.begin(), v.end()), v.end());
}


#line 2 "main.cpp"

long long solve(const vl &base, const vl &v1, const vl &v2) {
  // v1, v2でminを見る
  ll ans = 1e18;
  int len1 = v1.size();
  REP(i, len1) {
    auto idx = lower_bound(ALL(v2), v1[i]) - v2.begin();
    chmin(ans, abs(v1[i] - v2[idx]));
    if (idx > 0) {
      chmin(ans, abs(v1[i] - v2[idx - 1]));
    }
  }

  // baseをはさむ
  int lenb = base.size();
  ll bv1 = 1e18, bv2 = 1e18;

  if (lenb == 0) {
    return ans;
  }

  REP(i, lenb) {
    // dp1の更新
    auto idx = lower_bound(ALL(v1), base[i]) - v1.begin();
    if (idx < v1.size())
      chmin(bv1, abs(base[i] - v1[idx]));
    if (idx > 0) {
      chmin(bv1, abs(base[i] - v1[idx - 1]));
    }
  }

  REP(i, lenb) {
    // dp2の更新
    auto idx = lower_bound(ALL(v2), base[i]) - v2.begin();
    if (idx < v2.size())
      chmin(bv2, abs(base[i] - v2[idx]));
    if (idx > 0) {
      chmin(bv2, abs(base[i] - v2[idx - 1]));
    }
  }

  chmin(ans, bv1 + bv2);

  return ans;
}

// generated by online-judge-template-generator v4.7.1 (https://github.com/online-judge-tools/template-generator)
int main() {
  int N;
  std::cin >> N;
  vl r, g, b;
  REP(i, 2 * N) {
    ll a;
    char c;
    cin >> a >> c;
    if (c == 'R') {
      r.push_back(a);
    } else if (c == 'G') {
      g.push_back(a);
    } else {
      b.push_back(a);
    }
  }
  sort(ALL(r));
  sort(ALL(g));
  sort(ALL(b));

  int cr, cg, cb;
  cr = r.size();
  cg = g.size();
  cb = b.size();
  if (cr % 2 == 0 && cb % 2 == 0 && cg % 2 == 0) {
    cout << 0 << endl;
  } else if (cr % 2 == 0) {
    cout << solve(r, g, b) << endl;
  } else if (cg % 2 == 0) {
    cout << solve(g, r, b) << endl;
  } else if (cb % 2 == 0) {
    cout << solve(b, r, g) << endl;
  } else {
    assert(false);
  }
  return 0;
}

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <sys/types.h>
#include <unistd.h>
#include <vector>

#pragma region macros
#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) _range(i, 0, n)
#define _range(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__)
#define _rrep(i, n) _rrange(i, n, 0)
#define _rrange(i, a, b) for (int i = int(a) - 1; i >= int(b); --i)
#define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__)
#pragma endregion macros

using namespace std;

template <class T> bool chmax(T &a, const T &b) {
    return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
    return (b < a) ? (a = b, 1) : 0;
}

using ll    = long long;
using R     = long double;
const R EPS = 1e-9L;  // [-1000,1000]->EPS=1e-8 [-10000,10000]->EPS=1e-7
inline int sgn(const R &r) {
    return (r > EPS) - (r < -EPS);
}
inline R sq(R x) {
    return sqrt(max(x, 0.0L));
}

const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

const pid_t pid = getpid();
// Problem Specific Parameter:

int main(void) {
    int n, m;
    cin >> n >> m;
    vector<int> w(n);
    rep(i, n) cin >> w[i];

    vector<int> l(m), v(m);
    rep(i, m) {
        cin >> l[i] >> v[i];
    }

    const int wmax = *max_element(begin(w), end(w));
    rep(i, m) {
        if (wmax > v[i]) {
            cout << -1 << endl;
            return 0;
        }
    }


    using pii = pair<int, int>;
    vector<pii> bridge;
    rep(i, m) {
        bridge.push_back(pii(v[i], l[i]));
    }
    sort(begin(bridge), end(bridge));

    rep(i, 1, m) {
        chmax(bridge[i].second, bridge[i - 1].second);
    }
    bridge.erase(unique(begin(bridge), end(bridge)), end(bridge));

    m = bridge.size();


    // rep(i, m) {
    //     cerr << bridge[i].first << " " << bridge[i].second << endl;
    // }


    vector<int> ary(n);
    iota(begin(ary), end(ary), 0);

    const ll inf = 1LL << 30;
    ll ans       = inf;

    do {
        ll dist[8][8];
        rep(i, n) rep(j, n) {
            dist[i][j] = (i == j) ? 0 : inf;
        }

        // d_i <= d_{i + 1}
        rep(i, n - 1) {
            dist[i + 1][i] = 0;
        }

        rep(f, n) rep(t, f + 1, n) {
            int cur = 0;
            rep(i, f, t + 1) {
                cur += w[ary[i]];
            }

            auto it = lower_bound(begin(bridge), end(bridge), pii(cur, 0));
            if (it != begin(bridge)) {
                const int tar = prev(it)->second;
                chmin<ll>(dist[t][f], -tar);
            }
        }

        rep(k, n) rep(i, n) rep(j, n) {
            chmin(dist[i][j], dist[i][k] + dist[k][j]);
        }

        // cerr << "Dist:" << endl;
        // cerr << dist[0][n - 1] << endl;
        // cerr << dist[n - 1][0] << endl;

        chmin(ans, -dist[n - 1][0]);
    } while (next_permutation(begin(ary), end(ary)));

    cout << ans << endl;

    return 0;
}