#include #include #include using namespace std; const int maxn = 100009; int n , m; int a[maxn] , b[maxn]; int k[1009] , p[1009]; long long f[11][2009]; int max_hurt , max_def; void init() { bool flag[11]; memset(flag,false,sizeof(flag)); max_hurt = max_def = -1; for( int i = 1; i <= n; i++ ) scanf("%d%d",&a[i],&b[i]) , flag[ b[i] ] = true , max_def = max( max_def , b[i] ); for( int i = 1; i <= m; i++ ) scanf("%d%d",&k[i],&p[i]) , max_hurt = max( max_hurt , p[i] ); if ( max_def >= max_hurt ) return; memset(f,0x3f,sizeof(f)); for( int i = 0; i <= 10; i++ ) if ( flag[i] ) { f[i][0] = 0; for( int j = 1; j <= m; j++ ) if ( p[j] > i ) { for( int life = p[j]-i; life <= 2000; life++ ) f[i][life] = min( f[i][life] , min( f[i][life+1] , f[i][life-p[j]+i] + k[j] ) ); } for( int life = 2000; life >= 0; life-- ) f[i][life] = min( f[i][life] , f[i][life+1] ); } } int main() { while( scanf("%d%d",&n,&m) == 2 ) { init(); if ( max_def >= max_hurt ) { puts("-1"); continue; } long long ans = 0; for( int i = 1; i <= n; i++ ) ans += f[b[i]][a[i]]; printf("%I64d\n",ans); } return 0; }