#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <class T, class V>
ostream& operator<<(ostream& s, pair<T, V> a) {
  s << a.first << ' ' << a.second;
  return s;
}
pair<pair<long long int, long long int>, long long int> ara[2005];
int n, k;
long long int dp[2005];
long long int solve(int ind) {
  if (ind == n) return 0;
  if (dp[ind] != -1) return dp[ind];
  dp[ind] = 20000000000000000LL;
  long long int spent = 0, rem = k;
  for (int i = ind; i < n; i++) {
    long long int t =
        ara[i].second - rem - (ara[i].first.second - ara[i].first.first) * k;
    if (t > 0) break;
    if (rem > ara[i].second)
      rem -= ara[i].second;
    else if (t == 0) {
      if (i != n - 1 && ara[i + 1].first.first == ara[i].first.second)
        rem = 0;
      else
        rem = k;
    } else
      rem = k - (ara[i].second - rem) % k;
    spent += ara[i].second;
    if (i == n - 1 || ara[i + 1].first.first != ara[i].first.second || t <= -k)
      dp[ind] = min(
          dp[ind], solve(i + 1) + spent + ((rem == k || i == n - 1) ? 0 : rem));
  }
  return dp[ind];
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int T = 1;
  for (int qq = 1; qq <= T; qq++) {
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
      cin >> ara[i].first.first >> ara[i].first.second >> ara[i].second;
    }
    memset(dp, -1, sizeof dp);
    long long int ans = solve(0);
    if (ans >= 20000000000000000LL) ans = -1;
    cout << ans << endl;
  }
}
