#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
const long long MX = 2000 + 10;
const long long nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0};
template <class T>
using V = vector<T>;
template <class T>
bool ckmin(T& a, const T& b) {
  return a > b ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
  return a < b ? a = b, 1 : 0;
}
long long cdiv(long long a, long long b) {
  return a / b + ((a ^ b) > 0 && a % b);
}
constexpr long long log2(long long x) { return 31 - __builtin_clz(x); }
void IO() {}
long long N, K;
long long memo[2001][2001];
long long l[MX], r[MX], a[MX];
bool gap[MX];
long long nb[MX][MX];
void precompute() {
  for (long long l = (0); l < (N); ++l) {
    long long x = K;
    for (long long i = (l); i < (N); ++i) {
      nb[i][l] = x;
      x = (((x - a[i]) % K + K) % K);
    }
  }
}
long long solve(long long i, long long lst) {
  if (i == N) return 0;
  if (memo[i][lst] != -1) return memo[i][lst];
  long long x = nb[i][lst];
  long long t = 0;
  if (x < a[i]) {
    t = (cdiv((a[i] - x), K));
  }
  long long ans = INF;
  if (l[i] + t <= r[i]) ckmin(ans, solve(i + 1, lst) + a[i]);
  if (l[i] + t <= r[i] && gap[i])
    ckmin(ans, solve(i + 1, i + 1) + a[i] + nb[i + 1][lst]);
  if (l[i] + t < r[i]) ckmin(ans, solve(i + 1, i + 1) + a[i] + nb[i + 1][lst]);
  return memo[i][lst] = ans;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  IO();
  cin >> N >> K;
  for (long long i = (0); i < (N); ++i)
    for (long long j = (0); j < (N); ++j) memo[i][j] = -1;
  for (long long i = (0); i < (N); ++i) cin >> l[i] >> r[i] >> a[i];
  for (long long i = (0); i < (N); ++i) {
    if (i + 1 < N && r[i] < l[i + 1])
      gap[i] = true;
    else
      gap[i] = false;
  }
  precompute();
  long long ans = solve(0, 0);
  if (ans == INF)
    cout << -1 << "\n";
  else
    cout << ans << "\n";
  return 0;
}
