#include <bits/stdc++.h>
using namespace std;
long long dp[2039], l[2039], r[2039], a[2039];
long long n, k;
int main() {
  scanf("%lld%lld", &n, &k);
  for (int i = (1); i < n + 1; i++) scanf("%lld%lld%lld", &l[i], &r[i], &a[i]);
  for (int i = (n); i > 0; i--) {
    long long need = a[i];
    need += (r[i] == l[i + 1]) ? dp[i + 1] : 0;
    if ((r[i] - l[i] + 1) * k < need) {
      printf("-1");
      return 0;
    }
    need = max(0ll, need - (r[i] - l[i]) * k);
    dp[i] = need;
  }
  long long left = k, ans = 0;
  for (int i = (1); i < n + 1; i++) {
    if (left < dp[i]) {
      ans += left;
      left = k;
    }
    ans += a[i];
    left = ((left - a[i]) % k + k) % k;
  }
  printf("%lld", ans);
  return 0;
}
