#include <bits/stdc++.h>
using namespace std;
constexpr long long INF = (sizeof(long long) == 4 ? 1e9 : 2e18) + 1e5;
constexpr long long MOD = 1e9 + 7;
constexpr long long MAXN = 2e3 + 3;
vector<tuple<long long, long long, long long> > arr;
long long dp[MAXN][MAXN];
long long k;
long long n;
long long f(long long idx, long long last, long long cur) {
  if (idx == n) return -cur;
  long long &ret = dp[idx][last];
  if (ret != -INF) return ret;
  ret = INF;
  auto [l, r, w] = arr[idx];
  long long rel = (w - cur + k - 1) / k;
  if (rel > r - l) return INF;
  long long used = rel * k;
  long long nxt = used + cur - w;
  ret = min(ret, used + f(idx + 1, last, nxt));
  if (idx + 1 < n && l + rel < get<0>(arr[idx + 1])) {
    ret = min(ret, used + k + f(idx + 1, idx + 1, k));
  }
  return ret;
}
void solve() {
  cin >> n;
  for (long long i = 0; i <= n; ++i)
    for (long long j = 0; j <= n; ++j) dp[i][j] = -INF;
  cin >> k;
  arr.resize(n);
  for (auto &[a, b, c] : arr) {
    cin >> a >> b >> c;
  }
  long long ans = k + f(0, 0, k);
  cout << (ans >= INF ? -1 : ans) << '\n';
}
signed main() {
  const string FILE_NAME = "";
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
  long long t = 1;
  while (t--) solve();
}
