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