#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  template <class c>
  debug& operator<<(const c&) {
    return *this;
  }
};
mt19937 myrand(time(0));
int n, k;
int l[2002], r[2002], a[2002];
long long dp[2002][2];
long long call(int pos, int exact) {
  if (pos == n) return 0;
  long long& ret = dp[pos][exact];
  if (ret != -1) return ret;
  ret = 1e18;
  long long total = 0, rem = 0, prev = 0, last = 0;
  for (int i = pos; i < n; i++) {
    total = a[i] + rem;
    long long need = (total + k - 1) / k;
    if ((l[i] + need + last + (i == pos ? exact : 0) - 1) <= r[i]) {
      bool b = 0;
      if ((l[i] + need + last + (i == pos ? exact : 0) - 1) == r[i]) b = 1;
      if (i < n - 1) {
        long long now = call(i + 1, l[i + 1] == r[i] && b);
        ret = min(ret, now + prev + (need * k));
      } else {
        ret = min(ret, prev + (total % k == 0 ? need : need - 1) * k +
                           (total % k) + call(i + 1, 0));
      }
    } else
      break;
    long long taken = min(r[i] - l[i] + 1ll, total / k);
    long long cur = k * taken;
    prev += cur;
    rem = total - cur;
    if ((l[i] + last + taken + (i == pos ? exact : 0) - 1) == r[i])
      last = 1;
    else
      last = 0;
  }
  debug() << " ["
          << "pos"
             ": "
          << (pos)
          << "] "
             " ["
          << "ret"
             ": "
          << (ret) << "] ";
  return ret;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(), cout.tie();
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> l[i] >> r[i] >> a[i];
  memset(dp, -1, sizeof dp);
  long long ans = call(0, 0);
  if (ans >= 1e18)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
