#include using namespace std; typedef long long ll; #define pii pair ll dp[15][1085]; int num[15][1085]; pii s[1005]; int main() { int n, m, u, v; // memset(dp, 63, sizeof(dp)); // cout << dp[1][1] << endl << 0x3f3f3f3f3f3f3f3f << endl; while(scanf("%d%d", &n, &m) != EOF) { memset(num, 0, sizeof(num)); int mv = 0, mg = 0; for(int i = 1; i <= n; i++) { scanf("%d%d", &u, &v); mv = max(v, mv); num[v][u]++; } for(int i = 1; i <= m; i++) { scanf("%d%d", &s[i].first, &s[i].second); mg = max(mg, s[i].second); } if(mg <= mv) { puts("-1"); continue; } int flag = 0; ll ans = 0; memset(dp, 63, sizeof(dp)); for(int i = 0; i <= mv; i++) dp[i][0] = 0; for(int i = 0; i <= mv; i++) for(int j = 1; j <= 1000; j++) { // if(num[i][j] == 0) continue; // cout << i << " " << j << " ....\n"; for(int k = 1; k <= m; k++) { // if(s[k].second - i <= 0) continue; if(s[k].second - i >= j) { dp[i][j] = min(dp[i][j], s[k].first * 1LL); continue; } if(dp[i][j-(s[k].second - i)] == 0x3f3f3f3f3f3f3f3f) continue; dp[i][j] = min(dp[i][j], dp[i][j-(s[k].second - i)] + s[k].first); } if(dp[i][j] == 0x3f3f3f3f3f3f3f3f && num[i][j] != 0) { flag = 1; } ans = ans + 1LL * dp[i][j] * num[i][j]; } if(flag) printf("-1\n"); else cout << ans << endl; } return 0; }