#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T bigmod(T b, T p, T M) {
  T ret = 1;
  for (; p > 0; p >>= 1) {
    if (p & 1) ret = (ret * b) % M;
    b = (b * b) % M;
  }
  return (T)ret;
}
template <class T>
inline T modinverse(T a, T M) {
  return bigmod(a, M - 2, M);
}
template <class T>
inline T gcd(T a, T b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
template <class T>
inline T lcm(T a, T b) {
  return a * b / gcd<T>(a, b);
}
template <class T>
inline T power(T b, T p) {
  unsigned long long int x = 1;
  while (p--) x *= b;
  return x;
}
const int mod = 1000000007;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ;
  long long int n, k;
  cin >> n >> k;
  long long int L[n + 2], R[n + 2], Mons[n + 2], DP[n + 2];
  DP[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> L[i] >> R[i] >> Mons[i];
    DP[i] = 1e18;
  }
  for (int i = 1; i <= n; i++) {
    long long int rem = k, sumMons = DP[i - 1];
    for (int j = i; j <= n; j++) {
      long long int reloads = (Mons[j] - rem) / k;
      if ((Mons[j] - rem) % k != 0) reloads++;
      if (Mons[j] < rem) reloads = 0;
      if (reloads > R[j] - L[j]) break;
      sumMons += Mons[j];
      long long int newRem = rem + (reloads * k) - Mons[j];
      if (j == n)
        DP[j] = min(DP[j], sumMons);
      else if (L[j] + reloads < L[j + 1]) {
        DP[j] = min(DP[j], sumMons + newRem);
      }
      rem = newRem;
    }
  }
  if (DP[n] >= 1e18)
    cout << -1 << endl;
  else
    cout << DP[n] << endl;
  return 0;
}
