#include <bits/stdc++.h>
using namespace std;
const long long N = 4010;
inline long long read() {
  long long s = 0, w = 1;
  register char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar();
  return s * w;
}
long long n, K, F[N], Ans;
long long l[N], r[N], a[N];
signed main() {
  n = read(), K = read();
  for (register long long i = 1; i <= n; i++)
    l[i] = read(), r[i] = read(), a[i] = read();
  for (register long long i = n; i; i--) {
    long long gt = ((i < n && r[i] == l[i + 1]) ? (F[i + 1]) : (0));
    gt += a[i];
    if ((r[i] - l[i] + 1) * K < gt) return puts("-1") & 0;
    F[i] = max(0ll, gt - (r[i] - l[i]) * K);
  }
  long long now = K;
  for (register long long i = 1; i <= n; i++) {
    if (F[i] > now) {
      Ans += now;
      now = K;
    }
    Ans += a[i];
    now = (now - a[i]) % K;
    now = (now % K + K) % K;
  }
  printf("%lld\n", Ans);
  return 0;
}
