/*
ID: Λινδα
LANG: C++
TASK: abc194a
*/
#include <stdio.h>
#include <algorithm>
using namespace::std;
int read(){char c=getchar();int t=0,f=1;while(c<'0'||c>'9'){if(c=='-'){f=-1;}c=getchar();}while(c>='0'&&c<='9'){t=(t<<1)+(t<<3)+(c^48);c=getchar();}return t*f;}
void write(int x){char ch[50];int l=0;if(x<0){putchar('-');x=-x;}do{ch[l++]=x%10+'0';x=x/10;}while(x);while(l){putchar(ch[--l]);}}
struct wrkr
{
	int id,tm;
};
bool cmpr(wrkr x,wrkr y)
{
	return x.tm<y.tm;
}
int main()
{
//	freopen("main.in","r",stdin);
//	freopen("main.out","w",stdout);
//	freopen("main.err","w",stderr);
	int i,n=read();
	wrkr a[1004],b[1004];
	for(i=0;i!=n;++i)
	{
		a[i].tm=read();
		a[i].id=i;
		b[i].tm=read();
		b[i].id=i;
	}
	sort(a,a+n,cmpr);
	sort(b,b+n,cmpr);
	if(a[0].id==b[0].id)
	{
		write(min(a[0].tm+b[0].tm,min(max(a[0].tm,b[1].tm),max(a[1].tm,b[0].tm))));
	}
	else
	{
		write(max(a[0].tm,b[0].tm));
	}
	putchar('\n');
//	fclose(stdin);
//	fclose(stdout);
//	fclose(stderr);
	return 0;
}
#include<cstdio>
#include<vector>
int w[15];
struct Element{
    int l,v;
};
struct Edge{
    int v,w;
    Edge(int v,int w):v(v),w(w){}
};
std::vector<Edge>g[15];
int f[15];
Element e[100005];
int seq[15];
int vis[15];
int pos[15];
int ans = -1;
int dis[1<<10];

void dfs(int p,int n,int m){
    if(p==n+1){
        for(int i = 1; i <= n; i++){ 
            g[i].clear(), f[i] = 0;
            pos[seq[i]] = i;
        }
        for(int i = 0; i < (1<<n); i++){
            int l = n+1, r = -1;
            int mask = i;
            for(int j = 1; j <= n; j++){
                if(mask%2){
                    int p = pos[j];
                    if(l>p) l = p;
                    if(r<p) r = p;
                }
                mask /= 2;
            }
            if(l<=n && r>=1 && l<r) g[l].push_back(Edge(r,dis[i]));
        }
        for(int i = 1; i <= n; i++){
            if(f[i]<f[i-1]) f[i] = f[i-1];
            for(Edge e: g[i]){
                int j = e.v, w = e.w;
                if(f[j]<f[i]+w) f[j] = f[i]+w;
            }
        }
        if(ans==-1 || ans>f[n]) ans = f[n];
    }
    else{
        for(int i = 1; i <= n; i++){
            if(vis[i]==0){
                seq[p] = i;
                vis[i] = 1;
                dfs(p+1,n,m);
                vis[i] = 0;
            }
        } 
    }
}

int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    int maxn = -1;
    for(int i = 1; i <= n; i++){ 
        scanf("%d",&w[i]);
        if(maxn<w[i]) maxn = w[i];
    }
    int ok = 1;
    for(int i = 1; i <= m; i++){
        scanf("%d%d",&e[i].l,&e[i].v);
        if(e[i].v<maxn) ok = 0;
    }
    if(!ok){
        printf("-1\n");
        return 0;
    }
    for(int i = 0; i < (1<<n); i++){
        int sum = 0, mask = i;
        for(int j = 1; j <= n; j++){
            if(mask%2) sum += w[j];
            mask /= 2;
        }
        dis[i] = 0;
        for(int j = 1; j <= m; j++){
            if(e[j].v<sum && dis[i]<e[j].l) dis[i] = e[j].l;
        }
    }
    dfs(1,n,m);
    printf("%d\n",ans);
    return 0;
}
