先满足挑剔的
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
typedef long long ll;
int n, m, tmp, rot, cnt;
ll ans;
struct Cow{
int pri, gre;
}cow[100005];
struct Gra{
int pri, gre;
}gra[100005];
struct Treap{
int val[100005], hav[100005], l[100005], r[100005], rnd[100005];
void lRotate(int &k){
int t=r[k]; r[k] = l[t]; l[t] = k; k = t;
}
void rRotate(int &k){
int t=l[k]; l[k] = r[t]; r[t] = k; k = t;
}
void insert(int &k, int x){
if(!k){
k = ++cnt; hav[k] = 1;
val[k] = x; rnd[k] = rand();
return ;
}
if(val[k]==x) hav[k]++;
else if(val[k]<x){
insert(r[k], x);
if(rnd[r[k]]<rnd[k]) lRotate(k);
}
else{
insert(l[k], x);
if(rnd[l[k]]<rnd[k]) rRotate(k);
}
}
void queryNxt(int k, int x){
if(!k) return ;
if(val[k]>=x) tmp = val[k], queryNxt(l[k], x);
else queryNxt(r[k], x);
}
void del(int &k, int x){
if(!k) return ;
if(val[k]==x){
if(hav[k]>1){
hav[k]--;
return ;
}
if(l[k]*r[k]==0) k = l[k] + r[k];
else if(rnd[l[k]]<rnd[r[k]])
rRotate(k), del(k, x);
else
lRotate(k), del(k, x);
}
else if(val[k]<x) del(r[k], x);
else del(l[k], x);
}
}treap;
bool cmp1(Cow x, Cow y){
return x.gre>y.gre;
}
bool cmp2(Gra x, Gra y){
return x.gre>y.gre;
}
int main(){
cin>>n>>m;
for(int i=1; i<=n; i++)
scanf("%d %d", &cow[i].pri, &cow[i].gre);
for(int i=1; i<=m; i++)
scanf("%d %d", &gra[i].pri, &gra[i].gre);
sort(cow+1, cow+1+n, cmp1);
sort(gra+1, gra+1+m, cmp2);
int j=1;
for(int i=1; i<=n; i++){
tmp = -1;
while(j<=m && gra[j].gre>=cow[i].gre){
treap.insert(rot, gra[j].pri);
j++;
}
treap.queryNxt(rot, cow[i].pri);
if(tmp==-1){
cout<<"-1"<<endl;
return 0;
}
ans += tmp;
treap.del(rot, tmp);
}
cout<<ans<<endl;
return 0;
}