#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2010;
int n, k, l[MAXN], r[MAXN], p[MAXN];
long long f[MAXN];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%d%d%d", l + i, r + i, p + i);
  for (int i = n; i; i--) {
    if (r[i] == l[i + 1])
      f[i] = max(0ll, f[i + 1] + p[i] - 1ll * k * (r[i] - l[i]));
    else
      f[i] = max(0ll, 1ll * p[i] - 1ll * k * (r[i] - l[i]));
    if (f[i] > k) return 0 & puts("-1");
  }
  long long ans = 0, now = k;
  for (int i = 1; i <= n; i++) {
    if (now < f[i]) ans += now, now = k;
    ans += p[i];
    if (now >= p[i])
      now -= p[i];
    else
      now = k - (p[i] - now) % k;
  }
  printf("%lld\n", ans);
  return 0;
}
