#include <bits/stdc++.h>
using namespace std;
long long n, k, l[2005], r[2005], a[2005], f[2005];
signed main() {
  scanf("%lld%lld", &n, &k);
  for (long long i = 1; i <= n; i++) scanf("%lld%lld%lld", &l[i], &r[i], &a[i]);
  memset(f, 0x3f, sizeof f);
  f[1] = 0;
  for (long long i = 1; i <= n; i++) {
    long long now = k, ans = f[i];
    for (long long j = i; j <= n; j++) {
      long long ti = ((max(0ll, a[j] - now) + k - 1) / k);
      if (ti > r[j] - l[j]) break;
      long long nxt = (now + k * ti) - a[j];
      ans += a[j];
      if (j + 1 <= n) {
        if (l[j] + ti < l[j + 1]) f[j + 1] = min(f[j + 1], ans + nxt);
      } else {
        f[j + 1] = min(f[j + 1], ans);
      }
      now = nxt;
    }
  }
  if (f[n + 1] >= 0x3f3f3f3f3f3f3f3f)
    puts("-1");
  else
    printf("%lld\n", f[n + 1]);
  return 0;
}
