#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e3 + 5;
const int inf = 0x3f3f3f3f;
long long n, k;
long long l[maxn], r[maxn], a[maxn], dp[maxn];
int main() {
  scanf("%lld%lld", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%lld%lld%lld", &l[i], &r[i], &a[i]);
  }
  for (int i = n; i >= 1; i--) {
    long long need = a[i];
    if (r[i] == l[i + 1]) need += dp[i + 1];
    if (need > (r[i] - l[i] + 1) * k) {
      printf("-1\n");
      return 0;
    }
    dp[i] = max(0ll, need - (r[i] - l[i]) * k);
  }
  long long ans = 0, cur = k;
  for (int i = 1; i <= n; i++) {
    if (cur < dp[i]) {
      ans += cur;
      cur = k;
    }
    if (cur > a[i]) {
      cur -= a[i];
    } else {
      cur = k - (a[i] - cur) % k;
    }
    ans += a[i];
  }
  printf("%lld\n", ans);
  return 0;
}
