#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
//#include <atcoder/all>

#define F first
#define S second
#define int long long
#define ll long long
//#define int  unsigned long long
#define pb push_back
//#define double long double

using namespace std;
using namespace __gnu_pbds;
//using namespace atcoder;

typedef  tree< int , null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 55;
const int K = 20;
const int mod = 1e9 + 7;
int d[N][N];
main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int n, m;
    cin >> n >> m;
    vector <pair<int,int>> a(m);
    vector <int> f(m);
    vector <int> w(n), p(n);
    for (int i = 0; i < n; i++) cin >> w[i];
    for (int i = 0; i < n; i++) p[i] = i;
    for (int i = 0; i < m; i++){
        cin >> a[i].S >> a[i].F;
    }
    sort(a.begin(), a.end());
    for (int i = 0; i < m; i++){
        if (i != 0) f[i] = max(f[i - 1], a[i].S);
        else f[i] = a[i].S;
    }
    for (int i = 0; i < n; i++){
        if (w[i] > a[0].F){
            cout << -1;
            return 0;
        }
    }
    int res = 1e9;
    for (int i = 0; i < 40320; i++){
        next_permutation(p.begin(), p.end());
        int cur = 0;
        int cur_w = 0;
        for (int j = 0; j < n; j++){
            for (int l = j + 1; l < n; l++){
                int cur_w = 0;
                for (int k = j; k <= l; k++) cur_w += w[p[k]];
                if (cur_w <= a[0].F) d[j][l] = 0;
                else{
                    int z = (int)(lower_bound(a.begin(), a.end(), make_pair(cur_w, 0LL)) - a.begin()) - 1;
                    d[j][l] = f[z];
                }
            }
        }
        vector <int> pos(n);
        for (int j = 1; j < n; j++){
            pos[j] = pos[j - 1];
            for (int l = 0; l < j; l++){
                int need = d[l][j] - pos[j] + pos[l];
                if (need > 0) pos[j] += need;
            }
        }
        res = min(res, pos[n - 1]);
    }
    cout << res;
}

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
template<class S, class T> inline S max_L(S a,T b){
  return a>=b?a:b;
}
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(long long x){
  int s=0;
  int m=0;
  char f[20];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
template<class S, class T> inline S chmin(S &a, T b){
  if(a>b){
    a=b;
  }
  return a;
}
int N;
int X[20];
int Y[20];
int Z[20];
long long dp[131072][17];
long long dist[17][17];
int main(){
  int i, mask;
  rd(N);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){
      rd(X[Lj4PdHRW]);
      rd(Y[Lj4PdHRW]);
      rd(Z[Lj4PdHRW]);
    }
  }
  for(i=(0);i<(1<<N);i++){
    int j;
    for(j=(0);j<(N);j++){
      dp[i][j] = 4611686016279904256LL;
    }
  }
  for(i=(0);i<(N);i++){
    int j;
    for(j=(0);j<(N);j++){
      dist[i][j] = abs(X[i]-X[j]) + abs(Y[i]-Y[j]) +max_L(Z[j]-Z[i], 0);
    }
  }
  dp[0][0] = 0;
  for(mask=(0);mask<(1<<N);mask++){
    for(i=(0);i<(N);i++){
      if(dp[mask][i] < 4611686016279904256LL){
        int j;
        for(j=(0);j<(N);j++){
          if(!((mask) &(1<<(j)))){
            chmin(dp[mask|(1<<j)][j], dp[mask][i] + dist[i][j]);
          }
        }
      }
    }
  }
  wt_L(dp[(1<<N)-1][0]);
  wt_L('\n');
  return 0;
}
// cLay varsion 20201018-1

// --- original code ---
// int N, X[20], Y[20], Z[20];
// ll dp[131072][17], dist[17][17];
// {
//   rd(N,(X,Y,Z)(N));
//   rep(i,1<<N) rep(j,N) dp[i][j] = ll_inf;
//   rep(i,N) rep(j,N) dist[i][j] = abs(X[i]-X[j]) + abs(Y[i]-Y[j]) + max(Z[j]-Z[i], 0);
//   dp[0][0] = 0;
//   rep(mask,1<<N) rep(i,N) if(dp[mask][i] < ll_inf) rep(j,N) if(!BIT_ith(mask,j)){
//     dp[mask|(1<<j)][j] <?= dp[mask][i] + dist[i][j];
//   }
//   wt(dp[(1<<N)-1][0]);
// }
