#include <bits/stdc++.h>
using namespace std ;
typedef long long ll ;
#define M 100005
#define YES cout << "Yes" << endl 
#define NO cout << "No" << endl 
#define rep(i,n) for(int i = 0 ; i < n ; i++)
#define syosu setprecision(10)
#define inf (ll)1e15

int n , m ;

int main(){
    cin >> n >> m ;
    ll A[n] ;
    map<int,int> conf ;
    ll val = inf ;
    for(int i = 0 ; i < n ; i++){
        cin >> A[i] ;
        if(i < m) conf[A[i]]++ ;
    }
    for(ll i = 0 ; i <= n ; i++){
        if(conf[i] > 0) continue ;
        val = min(val,i) ;
    }

    int l = 0 , r = m - 1 ;
    while(r < n - 1){
        r++ ;
        conf[A[r]]++ ;
        if(conf[A[l]] == 1){
            val = min(val,A[l]) ;
        }
        conf[A[l]]-- ;
        l++ ;
    }
    cout << val << endl ;
}
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pii = pair<int, int>;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define per(i, b) per2(i, 0, b)
#define per2(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define ALL(c) (c).begin(), (c).end()
#define SZ(x) ((int)(x).size())

constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

template <class T, class U>
void chmin(T& t, const U& u) {
    if (t > u) t = u;
}
template <class T, class U>
void chmax(T& t, const U& u) {
    if (t < u) t = u;
}

template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << "(" << p.first << "," << p.second << ")";
    return os;
}

template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    os << "{";
    rep(i, v.size()) {
        if (i) os << ",";
        os << v[i];
    }
    os << "}";
    return os;
}

#ifdef LOCAL
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << H;
    debug_out(T...);
}
#define debug(...) \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

const ll INF = TEN(18);

int main() {
    int N, M;
    cin >> N >> M;
    V<ll> w(N);
    rep(i, N) cin >> w[i];

    V<int> pm(N);
    iota(ALL(pm), 0);
    V<pii> br(M);
    ll mx = 0;

    for (auto v : w) chmax(mx, v);
    rep(i, M) cin >> br[i].se >> br[i].fi;
    rep(i, M) if (mx > br[i].fi) {
        puts("-1");
        return 0;
    }

    map<int, int> D;

    int now = 0;
    V<int> xs;
    rep(i, 1 << N) {
        int s = 0;
        rep(j, N) if (i >> j & 1) { s += w[j]; }
        xs.pb(s);
    }
    sort(ALL(xs));
    sort(ALL(br));
    int md = 0;
    for (auto v : xs) {
        while (now < M && br[now].fi < v) {
            chmax(md, br[now++].se);
        }
        D[v] = md;
    }

    ll ans = INF;
    do {
        V<ll> a(N);
        rep(i, N) a[i] = w[pm[i]];
        V<ll> dp(N);

        rep(i, N) {
            int s = 0;
            if (i) chmax(dp[i], dp[i - 1]);
            for (int j = i; j < N; ++j) {
                s += a[j];
                if (j > i) chmax(dp[j], dp[i] + D[s]);
            }
        }
        chmin(ans, dp.back());
    } while (next_permutation(ALL(pm)));
    cout << ans << endl;
    return 0;
}