#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const long long INF = 1e18;
const double eps = 1e-10;
const double pi = acos(-1);
mt19937 mrand(chrono::high_resolution_clock::now().time_since_epoch().count());
template <class T>
inline void upmin(T &x, T y) {
  x = x > y ? y : x;
}
template <class T>
inline void upmax(T &x, T y) {
  x = x < y ? y : x;
}
const int maxn = 5000;
int l[maxn], r[maxn], a[maxn];
long long f[maxn];
int n, k;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) cin >> l[i] >> r[i] >> a[i];
  for (int i = n; i; i--) {
    int delta = a[i];
    if (i < n && l[i + 1] == r[i]) delta += f[i + 1];
    if (delta > 1ll * k * (r[i] - l[i] + 1)) {
      cout << "-1\n";
      return 0;
    }
    f[i] = max(0ll, delta - 1ll * k * (r[i] - l[i]));
  }
  long long res = 0;
  int now = k;
  for (int i = 1; i <= n; i++) {
    if (now < f[i]) {
      res += now;
      now = k;
    }
    res += a[i];
    now = ((now - a[i]) % k + k) % k;
  }
  cout << res << '\n';
  return 0;
}
