#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
signed main() {
  long long n, k;
  cin >> n >> k;
  vector<tuple<long long, long long, long long> > que;
  for (long long i = 0; i < n; ++i) {
    long long l, r, a;
    cin >> l >> r >> a;
    que.push_back(make_tuple(l, r, a));
  }
  unordered_map<long long, long long> dp, new_dp;
  dp[1] = k;
  long long prv = inf;
  for (long long i = 0; i < n; ++i) {
    long long l, r, a;
    tie(l, r, a) = que[i];
    long long can = (r - l);
    long long will = a / k + (a % k > 0);
    long long need;
    if (a % k)
      need = a % k + 1;
    else
      need = k;
    long long _will = will - 1;
    for (auto u : dp) {
      long long i = u.first, j = u.second;
      if (prv < l) {
        if (k >= a) {
          new_dp[i + 1] = max(new_dp[i + 1], k - a);
          if (1 <= can) new_dp[i + 2] = k;
        } else {
          long long X = a - k;
          long long y = X / k + (X % k > 0);
          if (y <= can)
            new_dp[i + y + 1] = max(new_dp[i + y + 1], (k - X % k) % k);
          if (y + 1 <= can) new_dp[i + y + 2] = k;
        }
      }
      if (j >= a) {
        new_dp[i] = max(new_dp[i], j - a);
        if (1 <= can) new_dp[i + 1] = max(new_dp[i + 1], k);
      } else {
        long long X = a - j;
        long long y = X / k + (X % k > 0);
        if (y <= can) new_dp[i + y] = max(new_dp[i + y], (k - X % k) % k);
        if (y + 1 <= can) new_dp[i + y + 1] = k;
      }
    }
    prv = r;
    swap(dp, new_dp);
    new_dp.clear();
  }
  long long ans = inf;
  for (auto u : dp) ans = min(ans, k * u.first - u.second);
  if (ans == inf)
    cout << -1;
  else
    cout << ans;
  return 0;
}
