#include #include #include #include using namespace std; typedef long long LL; const int N = 1e5 + 10; const int M = 1e3 + 10; int n, m, a[N], b[N], x[N], y[N]; int main() { while (scanf("%d %d", &n, &m) != EOF) { int mx = 0; for (int i = 1; i <= n; ++i) { scanf("%d %d", &a[i], &b[i]); mx = max(mx, a[i]); } for (int i = 1; i <= m; ++i) scanf("%d %d", &x[i], &y[i]); static LL g[N]; for (int _b = 0; _b <= 10; ++_b) { static LL f[M]; for (int i = 1; i <= mx; ++i) f[i] = -1; f[0] = 0; for (int i = 1; i <= mx; ++i) { LL nf = -1; for (int j = 1; j <= m; ++j) { if (y[j] > _b) { LL tf = f[max(0, i - (y[j] - _b))] + x[j]; if (nf == -1 || nf > tf) nf = tf; } } f[i] = nf; } for (int i = 1; i <= n; ++i) if (b[i] == _b) g[i] = f[a[i]]; } bool ok = 1; LL sum = 0; for (int i = 1; i <= n; ++i) { if (~g[i]) sum += g[i]; else ok = 0; } if (!ok) puts("-1"); else cout << sum << endl; } return 0; }