#include #include #include #include #include #include #include #include using namespace std; typedef long long ll; const int N = 1e5 + 5; int a[N], b[N]; int k[1005], p[1005]; int dp[11][2005]; int main() { int n,m; while(cin>>n>>m) { for(int i = 0; i < n ; i++) scanf("%d%d",&a[i], &b[i]); for(int i = 0; i < m; i++) scanf("%d%d",&k[i], &p[i]); memset(dp, -1, sizeof(dp)); for(int i = 0; i <= 10; i++) { dp[i][0] = 0; for(int o = 0; o < m; o++) { int gap = p[o] - i; if(gap > 0) { for(int j = 0; j < gap; j++) { if( -1 != dp[i][j]) { int cur = j + gap; while( cur < 2000 ) { if( -1 == dp[i][cur] || dp[i][cur - gap] + k[o] < dp[i][cur] ) { dp[i][cur] = dp[i][cur - gap] + k[o]; } cur += gap; } } } int mn = -1; int j; for(j = 2000; j >= 0; j--) { if( -1 != dp[i][j] ) { mn = dp[i][j]; break; } } for( j = j - 1; j >= 0; j--) { if( -1 == dp[i][j] || mn < dp[i][j] ) { dp[i][j] = mn; } else { mn = dp[i][j]; } } } } } ll ans = 0; for(int i = 0; i < n ; i++) { int cur = dp[b[i]][a[i]]; if( -1 == cur) { ans = -1; break; } else { ans += cur; } } //printf("%I64d\n",ans); cout<