#P2869. Gourmet Grass Purchase

    ID: 16127 Type: Default 1000ms 256MiB

Gourmet Grass Purchase

Gourmet Grass Purchase

Farmer John must purchase gourmet organic grass for his cows. Each cow i demands a grass type with price at least \(A_i\) and greenness score at least \(B_i\). The Green Grass Grocers (GGG) store offers \(M\) different grass types, each with price \(C_i\) and greenness \(D_i\). No two cows can have the same type of grass.

Your task is to assign each cow a unique grass type satisfying her requirements while minimizing the total cost. If such an assignment is impossible, output \(-1\).

inputFormat

The first line contains two space-separated integers \(N\) and \(M\) (1 ≤ \(N\) ≤ 100,000, 1 ≤ \(M\) ≤ 100,000), representing the number of cows and the number of grass types.

The next \(N\) lines each contain two integers \(A_i\) and \(B_i\) (1 ≤ \(A_i, B_i\) ≤ 10^9), the minimum price and greenness required by cow \(i\).

The following \(M\) lines each contain two integers \(C_i\) and \(D_i\) (1 ≤ \(C_i, D_i\) ≤ 10^9), representing the price and greenness of grass type \(i\).

outputFormat

Output a single integer representing the minimum total cost needed to satisfy all cows. If it is impossible to assign suitable grass types to all cows, output \(-1\).

sample

3 4
4 3
2 2
5 2
5 3
3 2
6 2
4 4
12