#include <set>
#include <cstdio>
#include <algorithm>
using namespace std;
char buf[10000000], *ptr = buf - 1;
inline int readint(){
int n = 0;
char ch = *++ptr;
while(ch < ‘0‘ || ch > ‘9‘) ch = *++ptr;
while(ch <= ‘9‘ && ch >= ‘0‘){
n = (n << 1) + (n << 3) + ch - ‘0‘;
ch = *++ptr;
}
return n;
}
const int maxn = 100000 + 10;
struct Node{
int a, b;
Node(){}
Node(int _a, int _b): a(_a), b(_b){}
};
class cmp1{
public:
bool operator () (const Node &x, const Node &y){
return x.a < y.a;
}
};
class cmp2{
public:
bool operator () (const Node &x, const Node &y){
return x.b < y.b;
}
};
Node x[maxn], y[maxn];
multiset<Node, cmp1> s;
int main(){
fread(buf, sizeof(char), sizeof(buf), stdin);
int n, m;
n = readint();
m = readint();
if(n > m){
puts("-1");
return 0;
}
for(int i = 1; i <= n; i++){
x[i].a = readint();
x[i].b = readint();
}
for(int i = 1; i <= m; i++){
y[i].a = readint();
y[i].b = readint();
}
sort(x + 1, x + n + 1, cmp2());
sort(y + 1, y + m + 1, cmp2());
long long ans = 0;
set<Node>::iterator it;
for(int i = n, j = m; i; i--){
while(j && y[j].b >= x[i].b){
s.insert(y[j]);
j--;
}
it = s.lower_bound(x[i]);
if(it == s.end()){
puts("-1");
return 0;
}
ans += (*it).a;
s.erase(it);
}
printf("%lld\n", ans);
return 0;
}