#include <bits/stdc++.h>
using i64 = long long;
#define REP(i, n) for (int i = 0, REP_N_ = int(n); i < REP_N_; ++i)
#define ALL(x) std::begin(x), std::end(x)

template <class T>
inline bool chmax(T &a, T b) {
  return a < b and ((a = std::move(b)), true);
}
template <class T>
inline bool chmin(T &a, T b) {
  return a > b and ((a = std::move(b)), true);
}

template <typename T>
std::istream &operator>>(std::istream &is, std::vector<T> &a) {
  for (auto &x : a) is >> x;
  return is;
}

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, m;
  cin >> n >> m;
  vector<int> w(n);
  cin >> w;
  map<int, int> vlmax;
  int lmax = 0;
  int vmin = 1e9;
  REP(i, m) {
    int l, v;
    cin >> l >> v;
    chmax(lmax, l);
    chmin(vmin, v);

    auto it = vlmax.lower_bound(v);
    if (it != vlmax.begin() and prev(it)->second >= l) {
      continue;
    }
    while (it != vlmax.end() and it->second <= l) {
      it = vlmax.erase(it);
    }
    chmax(vlmax[v], l);
  }
  if (vmin < *max_element(ALL(w))) {
    cout << -1 << endl;
    exit(0);
  }

  auto get_len = [&](int w) -> int {
    auto it = vlmax.lower_bound(w);
    if (it == vlmax.begin()) return 0LL;
    --it;
    return it->second;
  };

  vector<int> camels(n);
  REP(i, n) { camels[i] = i; }

  int ans = (n - 1) * lmax;
  do {
    vector<int> pos(n);
    for (int i = 1; i < n; ++i) {
      int w_sub = w[camels[i]];
      for (int j = i - 1; j >= 0; --j) {
        w_sub += w[camels[j]];
        int len = get_len(w_sub);
        chmax(pos[i], pos[j] + len);
      }
    }
    chmin(ans, pos[n - 1]);
  } while (next_permutation(ALL(camels)));

  cout << ans << endl;
}

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,f,n) for(ll i=(f); (i) < (n); i++)
#define repe(i,f,n) for(ll i=(f); (i) <= (n); i++)
#define repc(i,f,n) for(char i=(f); (i) <= (n); i++)
#define PI 3.14159265359
#define debug(x) cout<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";
#define P pair<int, int>
#define Pl pair<ll, ll>
#define dvec vector<vector<ll>>
#define OUT(x) cout << x << endl; return 0;
//printf("%.10f\n")
//cout << fixed << setprecision(10);
template<class T> inline bool chmax(T& a, T b){if (a < b) { a = b; return true; } return false;}
template<class T> inline bool chmaxe(T& a, T b){if (a <= b) { a = b; return true; } return false;}
template<class T> inline bool chmin(T& a, T b){if (a > b) { a = b; return true; } return false;}
const ll MOD = 1000000007ll;
const ll INF = 1e+18;
const int iINF = 1e9;
const double EPS = 1e-8;



int main()
{
  int N; cin >> N;
  string S; cin >> S;
  string cmp = "";
  int num = S.size();

  rep(i, 0, num){
    cmp.push_back(S[i]);
    int sz = cmp.size();
    if(sz >= 3 && cmp.substr(sz - 3) == "fox") rep(k, 0, 3) cmp.pop_back();
  }

  cout << cmp.size() << endl;
}