#include <bits/stdc++.h>
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
const long long MOD = 998244353;
const long long md = 11234549;
const long double PI = acosl(-1.0);
const long double E = 1e-15;
mt19937_64 rnd(time(0));
long long p1 = 227, p2 = 29 + rnd() % 100, md1 = MOD, md2 = MOD - rnd() % 1000;
long long n, m, k, T, dp[1000001];
vector<pair<pair<long long, long long>, long long> > v;
long long binpow(long long h, long long r, long long md) {
  long long l = 1;
  while (r) {
    if (r & 1) l *= h, l %= md;
    h *= h;
    h %= md;
    r /= 2;
  }
  return l;
}
void solve() {
  cin >> n >> k;
  v.push_back(make_pair(make_pair(0, 0), 0));
  for (int i = 1; i <= n; i++) {
    long long l, r, x;
    cin >> l >> r >> x;
    v.push_back(make_pair(make_pair(l, r), x));
    if ((r - l + 1) * k < x) {
      cout << "-1\n";
      return;
    }
  }
  v.push_back(make_pair(make_pair(1e18, 1e18 + 5), 0));
  for (int i = 2; i <= n + 1; i++) dp[i] = 1e18;
  for (int i = 1; i <= n; i++)
    if (dp[i] != 1e18) {
      long long o = k, sm = 0;
      for (int j = i; j <= n; j++) {
        if (v[j].second - o > (v[j].first.second - v[j].first.first) * k) break;
        long long p = max((v[j].second - o + k - 1) / k, 0ll);
        sm += v[j].second;
        o = (k - sm % k) % k;
        if (v[j].first.first + p < v[j + 1].first.first)
          dp[j + 1] = min(dp[j + 1], dp[i] + sm + (j != n ? o : 0));
      }
    }
  if (dp[n + 1] == 1e18)
    cout << "-1\n";
  else
    cout << dp[n + 1] << "\n";
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
