#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
long long n, k;
vector<long long> l, r, a;
vector<vector<long long>> dp;
long long solve(long long i, long long j, long long t) {
  if (i > n) {
    return -t;
  }
  if (dp[i][j] != -inf) return dp[i][j];
  long long re = (a[i - 1] - t + k - 1) / k;
  long long endtime = re + l[i - 1];
  if (endtime > r[i - 1]) {
    return dp[i][j] = inf;
  }
  long long b = t + re * k - a[i - 1];
  long long ans = a[i - 1] + solve(i + 1, j, b);
  if (i < n and endtime < l[i]) {
    ans = min(ans, re * k + t + solve(i + 1, i, k));
  }
  ans = min(ans, inf);
  ans = max(ans, a[i - 1]);
  return dp[i][j] = ans;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> k;
  l.resize(n);
  r.resize(n);
  a.resize(n);
  dp = vector<vector<long long>>(n + 1, vector<long long>(n + 1, -inf));
  for (long long i = 0; i < n; i++) {
    cin >> l[i] >> r[i] >> a[i];
  }
  solve(1, 0, k);
  if (dp[1][0] == inf) {
    dp[1][0] = -1;
  }
  cout << dp[1][0] << "\n";
  return 0;
}
