#include <bits/stdc++.h>
#pragma Ofast
using namespace std;
bool endline = false;
template <class T>
istream& operator>>(istream& inp, vector<T>& v) {
  for (auto& it : v) inp >> it;
  return inp;
}
template <class T>
ostream& operator<<(ostream& out, vector<T>& v) {
  for (auto& it : v) out << it << (endline ? "\n" : " ");
  return out;
}
template <class T, class U>
istream& operator>>(istream& inp, pair<T, U>& v) {
  inp >> v.first >> v.second;
  return inp;
}
template <class T, class U>
ostream& operator<<(ostream& out, pair<T, U>& v) {
  out << v.first << ' ' << v.second;
  return out;
}
const int mod1 = 998244353, mod = 1e9 + 7;
const int MAXN = 2e5 + 6, MAXM = 5e5 + 5;
const int inf = 2e9;
const long long linf = 1e18;
long long n, k, need[2020], l[2020], r[2020], a[2020];
void Solve() {
  cin >> n >> k;
  for (long long i = 0; i < n; i++) {
    cin >> l[i] >> r[i] >> a[i];
  }
  for (long long i = n - 1; i >= 0; i--) {
    need[i] = a[i] + (r[i] == l[i + 1] ? need[i + 1] : 0);
    if (k * (r[i] - l[i] + 1) < need[i]) {
      cout << -1 << endl;
      return;
    }
    need[i] = max(need[i] - (r[i] - l[i]) * k, 0ll);
  }
  long long ans = 0, cur = k;
  for (long long i = 0; i < n; i++) {
    if (cur < need[i]) ans += cur, cur = k;
    ans += a[i];
    cur = ((cur - a[i]) % k + k) % k;
  }
  cout << ans << endl;
}
signed main() {
  if (fopen(""
            ".inp",
            "r")) {
    freopen(
        ""
        ".inp",
        "r", stdin);
    freopen(
        ""
        ".out",
        "w", stdout);
  };
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long TC = 1;
  while (TC--) Solve();
  return 0;
}
