#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, k;
  cin >> n >> k;
  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, (1LL << 60));
  dp[0] = 0;
  long long ans = (1LL << 60);
  for (int i = 0; i < n; i++) {
    long long rem = k, total = dp[i];
    for (int j = i; j < n; j++) {
      long long tmp = max(0LL, a[j] - rem);
      long long reloads = tmp / k + (tmp % k > 0);
      if (reloads > r[j] - l[j]) break;
      long long newRem = reloads * k + rem - a[j];
      if (j < n - 1) {
        if (l[j] + reloads < l[j + 1] && total != (1LL << 60))
          dp[j + 1] = min(dp[j + 1], total + a[j] + newRem);
      } else {
        if (total != (1LL << 60)) ans = min(ans, total + a[j]);
      }
      if (total != (1LL << 60)) {
        total += a[j];
      }
      rem = newRem;
    }
  }
  if (ans == (1LL << 60))
    cout << "-1\n";
  else
    cout << ans << endl;
  return 0;
}
