#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  vector<int> l(n + 1), r(n + 1), a(n + 1);
  for (int i = 1; i <= n; i++) scanf("%d %d %d", &l[i], &r[i], &a[i]);
  bool ok = true;
  vector<int> need(n + 1, 0);
  for (int i = n; i >= 1 && ok; i--) {
    long long val = a[i];
    if (i < n && r[i] == l[i + 1]) val += need[i + 1];
    if (val > 1LL * (r[i] - l[i] + 1) * k)
      ok = false;
    else
      need[i] = max(0LL, val - 1LL * (r[i] - l[i]) * k);
  }
  if (!ok)
    printf("-1\n");
  else {
    long long val = 0, cnt = 0;
    for (int i = 1; i <= n; i++) {
      if (val < need[i]) cnt += val, val = k;
      cnt += a[i];
      val = ((val - a[i]) % k + k) % k;
    }
    printf("%lld\n", cnt);
  }
  return 0;
}
