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