#include <bits/stdc++.h>
using namespace std;
template <class T>
istream &operator>>(istream &in, pair<T, T> &p) {
  in >> p.first >> p.second;
  return in;
}
template <class T>
ostream &operator<<(ostream &out, pair<T, T> &p) {
  out << p.first << ' ' << p.second;
  return out;
}
template <class Tuple, std::size_t N>
struct TuplePrinter {
  static void print(ostream &out, const Tuple &t) {
    TuplePrinter<Tuple, N - 1>::print(out, t);
    out << ' ' << get<N - 1>(t);
  }
};
template <class Tuple>
struct TuplePrinter<Tuple, 1> {
  static void print(ostream &out, const Tuple &t) { out << get<0>(t); }
};
template <class... Args>
ostream &operator<<(ostream &out, const tuple<Args...> &p) {
  TuplePrinter<decltype(p), sizeof...(Args)>::print(out, p);
  return out;
}
int N, K;
long long dp[2005];
int l[2005], r[2005], a[2005];
int main() {
  ios_base::sync_with_stdio(0);
  cin >> N >> K;
  for (int i = 1; i <= N; i++) {
    cin >> l[i] >> r[i] >> a[i];
    dp[i + 1] = 1e18;
  }
  for (int i = 1; i <= N; i++) {
    long long rem = 0, nxtrem, res = 0;
    for (int j = i; j <= N; j++) {
      long long used = (a[j] + rem - 1) / K;
      if (used > r[j] - l[j]) {
        break;
      }
      res += used;
      nxtrem = a[j] + rem - used * K;
      if (l[j] + used < l[j + 1]) {
        dp[j + 1] = min(dp[j + 1], dp[i] + (res + (nxtrem > 0)) * K);
      }
      if (j == N) {
        dp[N + 1] = min(dp[N + 1], dp[i] + res * K + nxtrem);
      }
      rem = nxtrem;
    }
  }
  cout << (dp[N + 1] > 1e15 ? -1 : dp[N + 1]) << "\n";
}
