#include <bits/stdc++.h>
using namespace std;
int n, l[2010], r[2010];
long long dp[2010], k, a[2010];
long long cal() {
  for (int i = n; i >= 1; i--) {
    long long need = a[i];
    if (r[i] == l[i + 1]) need += dp[i + 1];
    if ((r[i] - l[i] + 1) * k < need) return -1;
    dp[i] = max(0LL, need - (r[i] - l[i]) * k);
  }
  int ammo = k;
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    if (ammo < dp[i]) sum += ammo, ammo = k;
    sum += a[i];
    ammo = ((ammo - a[i]) % k + k) % k;
  }
  return sum;
}
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) cin >> l[i] >> r[i] >> a[i];
  cout << cal() << endl;
  return 0;
}
