#include <bits/stdc++.h>
using namespace std;
template <class _Tp>
void ckmax(_Tp &a, _Tp b) {
  if (a < b) a = b;
}
template <class _Tp>
void ckmin(_Tp &a, _Tp b) {
  if (a > b) a = b;
}
template <class _Tp>
_Tp gcd(_Tp a, _Tp b) {
  return (b == 0) ? (a) : (gcd(b, a % b));
}
int read() {
  char ch = getchar();
  bool f = 1;
  int x = 0;
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') f = 0, ch = getchar();
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + (ch & 15);
    ch = getchar();
  }
  return f ? x : -x;
}
const int inf = 1000000000;
const long long Inf = 1000000000000000000ll;
int n, k, l[2050], r[2050], a[2050], dp[2050];
int main() {
  n = read();
  k = read();
  for (int i = 1; i <= n; ++i) {
    l[i] = read();
    r[i] = read();
    a[i] = read();
  }
  for (int i = n; i >= 1; --i) {
    long long need = a[i];
    if (i != n && r[i] == l[i + 1]) need += dp[i + 1];
    if (1ll * (r[i] - l[i] + 1) * k < need) {
      puts("-1");
      return 0;
    }
    dp[i] = max(0ll, need - 1ll * (r[i] - l[i]) * k);
  }
  long long ans = 0;
  int now = k;
  for (int i = 1; i <= n; ++i) {
    if (now < dp[i]) {
      ans += now;
      now = k;
    }
    ans += a[i];
    now = ((now - a[i]) % k + k) % k;
  }
  printf("%lld\n", ans);
  return 0;
}
