#include #define MAXN 100005 #define MAXM 1005 #define ll long long using namespace std; int n,m; int heath[MAXN],defence[MAXN],cost[MAXM],dam[MAXM]; ll dp[MAXM*2],best[2005][15]; int main() { while(cin >> n >> m) { int max_def = 0, max_hea=0,max_dam=0; for(int i=1; i<=n; i++) { scanf("%d%d",&heath[i],&defence[i]); max_def = max(max_def,defence[i]); max_hea = max(max_hea,heath[i]); } for(int i=1; i<=m; i++) { scanf("%d%d",&cost[i],&dam[i]); max_dam = max(max_dam,dam[i]); } if(max_dam<=max_def) { printf("-1\n"); continue; } memset(best,0x3f,sizeof best); for(int def=0; def<=max_def; def++) { best[0][def] = 0; for(int i=1; i<=m; i++) { if(dam[i]<=def) continue; for(int j=dam[i]-def; j<=max_hea+max_dam; j++) best[j][def] = min(best[j][def],best[j-dam[i]+def][def] + cost[i]); } } for(int def=0; def<=max_def; def++) for(int j=max_hea+max_dam; j>=1; j--) if(best[j][def]