#include #include #include #include #include #include #include #define lc(x) ns[x].l #define rc(x) ns[x].r using namespace std; typedef long long ll; typedef unsigned long long ull; struct io_s { bool negative; char ch; inline bool isdigitx(char c) { return c >= '0' && c <= '9'; } template bool rn(T& _v) { negative = false; _v = 0; while (!isdigitx(ch = getchar()) && ch != EOF) { negative = ch == '-'; }; if (ch == EOF)return false; do { _v = _v * 10 + ch - '0'; } while (isdigitx(ch = getchar())); if (negative) _v = -_v; return true; } template bool run(T& _v) { _v = 0; while (!isdigitx(ch = getchar()) && ch != EOF) {}; if (ch == EOF)return false; do { _v = _v * 10 + ch - '0'; } while (isdigitx(ch = getchar())); return true; } template inline T r() { T v = T(); rn(v); return v; } inline int ri() { return r(); } inline ll rll() { return r(); } template inline void o(T p) { static int stk[70], tp; if (p == 0) { putchar('0'); return; } if (p < 0) { p = -p; putchar('-'); } while (p) stk[++tp] = p % 10, p /= 10; while (tp) putchar(stk[tp--] + '0'); } inline void ln() { putchar('\n'); } template inline void oln(T p) { o(p); ln(); } } io; const int N = 100000 + 10; const int M = 1000 + 10; const int K = 10 + 1; const int MOD = 998244353; int n, m, mx, my; int a[N], b[N]; int c[N], d[N]; int mi[K][N]; inline void update(int& x, int y) { if (y < x) x = y; } int main() { while (io.rn(n) && io.rn(m)) { mx = 0; my = 0; for (int i = 0; i < n; ++i) { io.rn(a[i]), io.rn(b[i]); if (b[i] > my)my = b[i]; } for (int i = 0; i < m; ++i) { io.rn(c[i]), io.rn(d[i]); if (d[i] > mx)mx = d[i]; } if (mx <= my) { puts("-1"); continue; } memset(mi, 127, sizeof mi); for (int k = 0; k <= 10; ++k) { mi[k][0] = 0; for (int i = 0; i < m; ++i) { int x = d[i] - k, y = c[i]; if (x <= 0)continue; for (int j = 0; j< M; ++j) { update(mi[k][min(j + x, M - 1)], mi[k][j] + y); } } for (int i = M - 2; i >= 0; --i) { update(mi[k][i], mi[k][i + 1]); } } ll ans = 0; for (int i = 0; i < n; ++i) { ans += mi[b[i]][a[i]]; } io.oln(ans); } return 0; }