#include #include #include #include using namespace std; int n,m,mx1,ss,mx2,ok,now,x,i,j,k; int long long sum; int a[200005],b[200005],K[200005],p[200005],dp[15][2005]; int main() { while (scanf("%d %d",&n,&m)!=EOF) { mx1=mx2=ss=0; for (i=1;i<=n;i++) { scanf("%d%d",&a[i],&b[i]); mx1=max(mx1,b[i]); ss=max(ss,a[i]); } for (i=1;i<=m;i++) { scanf("%d%d",&K[i],&p[i]); mx2=max(mx1,p[i]); } //if (mx1>=mx2){puts("-1");continue;} for (i=0;i<=mx1;i++) for (j=0;j<=ss;j++) dp[i][j]=2e9; for (i=0;i<=mx1;i++)dp[i][0]=0; for (i=0;i<=mx1;i++) for (j=0;j=0;j--) dp[i][j]=min(dp[i][j],dp[i][j+1]); sum=0;ok=0; for (i=1;i<=n;i++) { if (dp[b[i]][a[i]]==2e9){ok=1;continue;} sum+=dp[b[i]][a[i]]; } if (ok) puts("-1");else printf("%I64d\n",sum); } }