#include #include #include #include #include #include #include #include #include #include #include #include #define k first #define p second #define clr(u,v); memset(u,v,sizeof(u)); #define in() freopen("data","r",stdin); #define out() freopen("ans","w",stdout); #define Clear(Q); while (!Q.empty()) Q.pop(); #define pb push_back using namespace std; typedef long long ll; typedef pair pii; const int maxn = 1e5 + 10; const ll INF = 1e17; struct node { int a, b; bool operator < (const node &A) const { if (b != A.b) return b < A.b; return a > A.a; } } N[maxn]; ll dp[2010]; pii M[1010]; int main() { #ifdef LOCAL in(); #endif int n, m; while (~scanf("%d%d", &n, &m)) { for (int i = 0; i < n; i++) { scanf("%d%d", &N[i].a, &N[i].b); } for (int i = 0; i < m; i++) { scanf("%d%d", &M[i].k, &M[i].p); } sort(N, N + n); int pos = 0; ll ans = 0; int ok = 1; for (int b = 0; b <= 10 && ok; b++) { for (int i = 0; i <= 2000; i++) dp[i] = INF; dp[0] = 0; for (int i = 0; i < m; i++) { int x = max(0, M[i].p - b); for (int j = 0; j <= 2000; j++) dp[j] = min(dp[j], dp[j-x] + M[i].k); } int st = 2000; ll Min = INF; while (pos < n && N[pos].b <= b) { while (st >=0 && st >= N[pos].a) { Min = min(Min, dp[st]); st--; } pos++; if (Min == INF) { ok=0; break; } ans += Min; } } if (ok==0) ans=-1; printf("%I64d\n", ans); } return 0; }