#include <bits/stdc++.h>
using namespace std;
constexpr int64_t INF = 1e18;
int n, k;
vector<vector<int64_t>> dp;
vector<array<int, 3>> waves;
int64_t gogo(int idx, int last, int cur_mag) {
  if (idx == n) return -cur_mag;
  if (dp[idx][last] != -INF) return dp[idx][last];
  int reloads = (waves[idx][2] - cur_mag + k - 1) / k;
  if (waves[idx][1] - waves[idx][0] < reloads) return INF;
  int64_t used = (int64_t)reloads * k;
  dp[idx][last] = used + gogo(idx + 1, last, used + cur_mag - waves[idx][2]);
  if (idx + 1 < n && waves[idx][0] + reloads < waves[idx + 1][0])
    dp[idx][last] = min(dp[idx][last], used + k + gogo(idx + 1, idx + 1, k));
  return dp[idx][last];
}
int main() {
  ios::sync_with_stdio(false), cin.tie(nullptr);
  cin >> n >> k;
  waves.resize(n);
  dp.assign(n, vector<int64_t>(n, -INF));
  for (int i = 0; i < n; i++) {
    for (int &it : waves[i]) {
      cin >> it;
    }
  }
  int64_t ans = k + gogo(0, 0, k);
  cout << (ans >= INF ? -1 : ans) << '\n';
  return 0;
}
