Package Delivery

Johnny drives a truck and must deliver a package from his hometown to the district center. His hometown is located at point 0 on a number line, and the district center is located at the point d.

Johnny’s truck has a gas tank that holds exactly n liters, and his tank is initially full. As he drives, the truck consumes exactly one liter per unit distance traveled. Moreover, there are m gas stations located at various points along the way to the district center. The i-th station is located at the point x i on the number line and sells an unlimited amount of fuel at a price of p i dollars per liter. Find the minimum cost Johnny must pay for fuel to successfully complete the delivery.

Input

The first line of input contains three space separated integers dn, and m (1 ≤ n ≤ d ≤ 109, 1 ≤ m ≤ 200 000) — the total distance to the district center, the volume of the gas tank, and the number of gas stations, respectively.

Each of the next m lines contains two integers x ip i (1 ≤ x i ≤ d - 1, 1 ≤ p i ≤ 106) — the position and cost of gas at the i-th gas station. It is guaranteed that the positions of the gas stations are distinct.

Output

Print a single integer — the minimum cost to complete the delivery. If there is no way to complete the delivery, print -1.

Examples

input

10 4 4
3 5
5 8
6 3
8 4

output

22

input

16 5 2
8 2
5 1

output

-1

Note

In the first sample, Johnny’s truck holds 4 liters. He can drive 3 units to the first gas station, buy 2 liters of gas there (bringing the tank to 3 liters total), drive 3 more units to the third gas station, buy 4 liters there to fill up his tank, and then drive straight to the district center. His total cost is 2·5 + 4·3 = 22 dollars.

In the second sample, there is no way for Johnny to make it to the district center, as his tank cannot hold enough gas to take him from the latest gas station to the district center.

Solution:

#include <bits/stdc++.h>

using namespace std;

const int N = 1234567;

pair <int, int> ev[N];

long long x[N], p[N];
long long st[N], pref[N];

int main() {
int dist, b, n;
scanf("%d %d %d", &dist, &b, &n);
for (int i = 0; i < n; i++) {
    scanf("%d %d", &ev[i].first, &ev[i].second);
  }
  ev[n++] = make_pair(0, 0);
  ev[n++] = make_pair(dist, 0);
  sort(ev, ev + n);
  for (int i = 0; i < n; i++) {
    x[i] = ev[i].first;
    p[i] = ev[i].second;
  }
  for (int i = 0; i < n - 1; i++) {
    if (x[i + 1] - x[i] > b) {
cout << -1 << endl;
      return 0;
    }
  }
  long long ans = 0;
  pref[0] = 0;
  int e = 0;
  for (int i = 0; i < n - 1; i++) {
    long long need = x[i + 1] - x[i];
    while (e > 0 && p[i] <= p[st[e - 1]]) {
      e--;
    }
    st[e] = i;
    if (e >= 1) {
pref[e] = pref[e - 1] + (x[st[e]] - x[st[e - 1]]) * p[st[e]];
}
e++;
int low = 0, high = e - 1;
while (low < high) {
      int mid = (low + high) >> 1;
if (x[st[mid]] <= x[i] - b) {
        low = mid + 1;
      } else {
        high = mid;
      }
    }
    int pos = low;
    long long can = b - (x[i] - x[st[pos]]);
    if (can >= need) {
ans += need * p[st[pos]];
continue;
}
ans += can * p[st[pos]];
need -= can;
low = pos + 1, high = e - 1;
while (low < high) {
      int mid = (low + high) >> 1;
if (x[st[mid]] - x[st[pos]] >= need) {
high = mid;
} else {
low = mid + 1;
}
}
ans += pref[low - 1] - pref[pos];
need -= x[st[low - 1]] - x[st[pos]];
ans += p[st[low]] * need;
}
cout << ans << endl;
  return 0;
}