Problem: problem

AOR Co., Ltd. (Association Of Return home) has $ N $ employees.

Employee $ i $ wants to use the elevator to go down to the $ 1 $ floor and arrives in front of the elevator on the $ F_i $ floor at time $ t_i $. You decide to remotely control an elevator that has only $ 1 $ on the $ 1 $ floor at time $ 0 $ and send all employees to the $ 1 $ floor. The elevator can only carry up to $ D $ people. Elevators can move upstairs or stay in place per unit time. Employee $ i $ only takes the elevator if the elevator is on the $ F_i $ floor at time $ t_i $ and does not exceed capacity. When I can't get on the elevator at time $ t_i $, I'm on the stairs to the $ 1 $ floor. The time it takes to get on and off can be ignored.

Find the minimum total time each employee is in the elevator. However, if there are even $ 1 $ people who cannot take the elevator to the $ 1 $ floor, output $ -1 $.



output

Output the minimum total time each employee is in the elevator. However, if there are even $ 1 $ people who cannot take the elevator to the $ 1 $ floor, output $ -1 $. Also, output a line break at the end.

Example

Input

2 2
2 2
3 3


Output

5

Solution (in CPP):

Solution:
#include <bits/stdc++.h>

using namespace std;

#define int long long

signed main() {
  int N, D;
  cin >> N >> D;
  vector<int> t(N), F(N);
  for(int i = 0; i < N; i++) {
    cin >> t[i] >> F[i];
  }
  if(t[0] < F[0]-1) {
    cout << -1 << endl;
    return 0;
  }
  int ans = 0, cap = 1;
  for(int i = 1; i < N; i++) {
    if(t[i-1]+(F[i-1]-1)+(F[i]-1) <= t[i]) {
      ans += (F[i-1]-1)*cap;
      cap = 1;
    } else if(t[i-1]+llabs(F[i-1]-F[i]) <= t[i]) {
      ans += (t[i]-t[i-1])*cap;
      cap++;
      if(cap > D) {
	cout << -1 << endl;
	return 0;
      }
    } else {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << ans+(F[N-1]-1)*cap << endl;


  return 0;
}