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