#include<bits/stdc++.h>
#define endl ("\n")
#define int long long
#define read(a,n) rep(i,0,n){cin>>a[i];}
#define print(a,n) rep(i,0,n) cout<<a[i]<<" ";
#define yy cout<<"Yes"<<endl;
#define nn cout<<"No"<<endl;
#define rep(i, a, n) for(int i = a; i < n; i++)
#define ff first
#define ss second
#define mod 1000000007
using namespace std;




int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T = clock();
    int t = 1 ;
    // cin >> t ;
    while (t--)
    {
        int n, k, a, b;
        cin >> n >> k;
        map<int, int> m;
        int tb = k;
        rep(i, 0, n)
        {
            cin >> a >> b;
            m[a] += b;

        }
        for (auto it : m) {

            a = it.ff;
            b = it.ss;
            if (tb < a)
            {
                break;
            }

            tb += b;
        }

        cout << tb << endl;

    }
    
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define all(v) v.begin(), v.end()
using P = pair<int,int>;
constexpr int inf = numeric_limits<int>::max() / 2;
int main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	int n, m; cin >> n >> m;
	vector<int> w(n); rep(i,n) cin >> w[i];
	vector<P> b(m+1);
	rep(i,m){
		int l, v; cin >> l >> v;
		b[i] = {v,l};
	}
	b[m] = {0,0};
	sort(all(b));
	int e = 1;
	for(int i=1; i<=m; i++){
		if(b[i].first == b[e-1].first) b[e-1].second = b[i].second;
		else if(b[i].second > b[e-1].second) b[e++] = b[i];
	}
	int ans = inf;
	vector<int> v(n); iota(all(v), 0);
	rep(i,n) if(b[1].first < w[i]) return 0 * puts("-1");
	do {
		vector<int> len(n,0), ac(n+1,0);
		rep(i,n) ac[i+1] = ac[i] + w[v[i]];
		rep(i,n) for(int j=i+1; j<n; j++){
			int l = len[i] + (--lower_bound(b.begin(), b.begin()+e, P{ac[j+1]-ac[i],0}))->second;
			len[j] = max(len[j], l);
		}
		ans = min(ans, len[n-1]);
	}while(next_permutation(all(v)));
	cout << ans << "\n";
}