#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
long long n, k;
unordered_map<long long, long long> dp[2005];
long long solve(pair<pair<long long, long long>, long long> ar[], long long ind,
                long long bullets) {
  if (ind == n) return 0ll;
  if (dp[ind].find(bullets) != dp[ind].end()) return dp[ind][bullets];
  long long monsters = ar[ind].second;
  long long time = ar[ind].first.second - ar[ind].first.first;
  long long reload = (monsters - bullets) / k;
  if (monsters > bullets && (monsters - bullets) % k) reload++;
  long long req_time = reload;
  if (req_time > time) {
    return 1e15;
  }
  long long used = monsters;
  long long remaining = bullets + reload * k - monsters;
  long long ans1 = solve(ar, ind + 1, remaining) + used;
  long long ans2 = 1e15;
  if (ind + 1 != n) {
    long long end_time = ar[ind].first.first + req_time;
    if (end_time < ar[ind + 1].first.first) {
      ans2 = solve(ar, ind + 1, k) + used + remaining;
    }
  }
  return dp[ind][bullets] = min(ans1, ans2);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  pair<pair<long long, long long>, long long> ar[n];
  for (int i = 0; i < n; i++)
    cin >> ar[i].first.first >> ar[i].first.second >> ar[i].second;
  long long ans = solve(ar, 0, k);
  if (ans >= 1e15) ans = -1;
  cout << ans << "\n";
  return 0;
}
