#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
std::mt19937 rnd(233);
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pLL;
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define ls (i<<1)
#define rs (i<<1|1)
#define mem(a,b) memset(a,b,sizeof(a))
const int N=1e6+5;
const int inf=0x3f3f3f3f;
const LL mod=1e9+7;
LL read()
{
    LL x=0,f=1;
    char ch=getchar();
    while(!isdigit(ch)){ if(ch=='-') f=-1; ch=getchar(); }
    while(isdigit(ch)){ x=10*x+ch-'0'; ch=getchar(); }
    return f*x;
}
pii a[N];
int l[N],w[N],b[N];
LL dp[10],sum[10];
int main()
{
    int n=read(),m=read(),ma=0,mi=1e9;
    for(int i=1;i<=n;i++) w[i]=read(),ma=max(ma,w[i]);
    for(int i=1;i<=m;i++)
    {
        a[i].se=read();
        a[i].fi=read();
        mi=min(mi,a[i].fi);
    }
    if(mi<ma) return 0*printf("-1\n");
    sort(a+1,a+m+1);
    for(int i=1;i<=m;i++) l[i]=max(a[i].se,l[i-1]);
    sort(w+1,w+n+1);
    LL ans=1e18;
    do
    {
        //for(int i=1;i<=n;i++) printf("%d%c",w[i],i==n?'\n':' ');
        mem(dp,0);
        for(int i=1;i<=n;i++) sum[i]=sum[i-1]+w[i];
        for(int i=1;i<=n;i++)
        for(int j=1;j<i;j++)
        {
            int tmp=sum[i]-sum[j-1];
            int pos=lower_bound(a+1,a+m+1,mk(tmp,0))-a-1; //printf("%d\n",pos);
            dp[i]=max(dp[i],dp[j]+l[pos]);
        }
        ans=min(dp[n],ans);
    }while(next_permutation(w+1,w+n+1));
    printf("%lld\n",ans);
    return 0;
}

#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#pragma GCC optimize("Ofast")
//*********************************************0123456789****************************************************************
#include<bits/stdc++.h> 
#define int long long   
#define pp pair<int,int> 
#define ss second 
#define ff first 
#define all(v) (v).begin(),(v).end()
#define pb push_back
#define mod 1000000007
#define pi 3.14159265359
#define mk(arr,n,type)  type *arr=new type[n];
#define sl s1.length();
#define yes cout<< "Yes"<<endl
#define no  cout<< "No"<<endl
#define all(v) (v).begin(),(v).end()
#define s(v)   sort(v,v+n)
#define r(v) reverse(v,v+n)
#define read(a,n) for(int i=0;i<n;i++)cin>>a[i]
#define print(a,n) for(int i=0;i<n;i++)cout<<a[i]<<" "
#define for1(i,a,n)  for(int i=0;i<n;i++)
#define rep(i, c) for(int i = 0; i < (int)c; i++)
#define set(x) cout<<setprecision(x)<<fixed;
#define cone(x) (__builtin_popcountll(x))        // zero indexed
#define fone(x) (__builtin_ffsll(x) -1LL)        //give wrong answer if x==0;(zero indexed)
#define mone(x) (63LL-__builtin_clzll(x)) 
#define  mt mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define RUN ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define debug(output) cout<<#output<<"="<<output<<endl
using namespace std;
std::vector<int>adj[10000];
bool visited[10000];
string str;
int getMaxGCD(int arr[], int n) {
   int high = 0;
   for (int i = 0; i < n; i++)
   high = max(high, arr[i]);
   int divisors[high + 1] = { 0 }; //array to store all gcd values
   for (int i = 0; i < n; i++) {
      for (int j = 1; j <= sqrt(arr[i]); j++) {
         if (arr[i] % j == 0) {
            divisors[j]++;
         if (j != arr[i] / j)
            divisors[arr[i] / j]++;
         }
      }
   }
   for (int i = high; i >= 1; i--)
   if (divisors[i] > 1)
   return i;
}
signed main()
{   
    RUN;
    int test=1;
    //cin>>test;
    while(test--)
    {  
      int a,b;
      cin>>a>>b;
      int ar[1000000],j=0;
      for(int i=a;i<=b;i++)
      ar[j]=i,j++;
      int ans=getMaxGCD(ar,j);
      cout<<ans;
      
    }
}