#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long n, k;
  cin >> n >> k;
  vector<long long> l(n), r(n), a(n);
  for (int i = 0; i < n; i++) {
    cin >> l[i] >> r[i] >> a[i];
  }
  vector<long long> dp(n + 1);
  for (int i = n - 1; i >= 0; i--) {
    long long d = a[i];
    if (i + 1 < n && r[i] == l[i + 1]) {
      d += dp[i + 1];
    }
    if ((r[i] - l[i] + 1) * k < d) {
      cout << -1 << endl;
      return 0;
    }
    dp[i] = max(0ll, d - (r[i] - l[i]) * k);
  }
  long long ans = 0, c = 0;
  for (int i = 0; i < n; i++) {
    if (c < dp[i]) {
      ans += c;
      c = k;
    }
    ans += a[i];
    c = ((c - a[i]) % k + k) % k;
  }
  cout << ans << endl;
  return 0;
}
