#include <bits/stdc++.h>
using namespace std;
void setIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}
const int MX = 2001;
const long long INF = 1e15;
long long l[MX], r[MX], a[MX];
long long n, k;
long long mem[MX];
long long f(int i) {
  if (i == n) return -k;
  long long &ans = mem[i];
  if (ans != -INF) return ans;
  ans = INF;
  long long ks = 0;
  long long kr = k;
  for (int j = i; j < n; j++) {
    long long ar = a[j];
    long long p = min(kr, ar);
    ar -= p;
    kr -= p;
    p = (ar + k - 1) / k;
    long long nks = p * 1ll * k;
    kr += nks - ar;
    ks += nks;
    if (l[j] + p > r[j]) {
      return ans;
    } else if (l[j] + p < l[j + 1]) {
      ans = min(ans, ks + k + f(j + 1));
    }
  }
  ans = min(ans, ks - kr);
  return ans;
}
long long solve() {
  for (int _n(n + 1), i(0); i < _n; i++) mem[i] = -INF;
  long long r = k + f(0);
  return r >= INF ? -1 : r;
}
int main() {
  setIO();
  cin >> n >> k;
  for (int _n(n), i(0); i < _n; i++) {
    cin >> l[i] >> r[i] >> a[i];
  }
  l[n] = r[n] = r[n - 1] + 1;
  cout << solve() << "\n";
  return 0;
}
