#include #include #include #include #include #include #include #include #include #include #include using namespace std; #define mem(x,y) memset(x,y,sizeof(x)) #define pb push_back #define bug puts("==========="); #define REP(i,n) for(int i=0;i pii; #define CHG ch=getchar() #define FRD x=bo=0; for(CHG;ch<'0'||ch>'9';CHG) if(ch=='-')bo=1; #define FR2 for(;ch>='0'&&ch<='9';x=(x<<1)+(x<<3)+ch-'0',CHG); char ch; int bo; inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';} inline void RD(int &x){ FRD FR2 if (bo)x=-x; } inline void RD(ll &x){ FRD FR2 if (bo)x=-x; } inline ll RD(){ ll x; RD(x); return x; } inline void RD(char *s){///scanf %s for (CHG;blank(ch);CHG); for (;!blank(ch);CHG)*s++=ch; *s=0; } inline void RD(char &c){for(CHG;blank(c);CHG);} template inline void OT(T x){ static char buf[20]; char *p1=buf;if (!x)*p1++='0';if (x<0)putchar('-'),x=-x; while(x)*p1++=x%10+'0',x/=10; while(p1--!=buf)putchar(*p1); } inline void pe(){puts("");}//输出回车 inline void pk(){putchar(' ');}//输出空格 const double eps=1e-8; const int inf=0x3f3f3f3f; ll mod=1e9+7; const int maxn=1e5+50; /*======================================*/ struct node{ ll k,p; void in(){ RD(k),RD(p); } bool operator<(const node &t)const{ if(p!=t.p)return pt.k; } }e[maxn],a[maxn]; ll dp[1000+50]; int main(){ int n,m; while(~scanf("%d%d",&n,&m)){ ll ans=0; int fl=0; REP(i,n)a[i].in(); REP(i,m)e[i].in(); sort(a,a+n); sort(e,e+m); REP(i,n){ int lim=(int)a[i].k; if(i&&a[i].p==a[i-1].p) goto lab; mem(dp,-1); dp[0]=0; for(int j=m-1;j>=0;j--){ int w=(int)(e[j].p-a[i].p); //OT(w),pe(); if(w<=0)break; for(int k1=0;k1<=lim;k1++)if(dp[k1]!=-1){ int now=min(k1+w,lim); if(dp[now]==-1)dp[now]=dp[k1]+e[j].k; else dp[now]=min(dp[now],dp[k1]+e[j].k); } } dp[lim+1]=1e12; for(int j=lim;j>=0;j--){ if(dp[j]==-1)dp[j]=dp[j+1]; else dp[j]=min(dp[j],dp[j+1]); //printf("%I64d ",dp[j]); } // puts(""); lab: if(dp[lim]!=1e12)ans+=dp[lim]; else fl=1; } if(fl)puts("-1"); else printf("%I64d\n",ans); } return 0; }