#include <bits/stdc++.h>
using namespace std;
long long L[2005];
long long R[2005];
long long a[2005];
long long N, K;
long long dp[2005][2005];
long long go(int index, int last, long long bullets) {
  if (index == N) {
    return 0;
  }
  if (dp[index][last] >= 0) {
    return dp[index][last];
  }
  long long zombies = a[index];
  long long delta = R[index] - L[index];
  long long left = min(zombies, bullets);
  long long mn = (1LL << 60);
  long long reloads = 0;
  long long reloads_used;
  long long shots = 0;
  zombies -= left;
  bullets -= left;
  shots += left;
  reloads = (zombies + K - 1) / K;
  reloads_used = min(reloads, delta);
  bullets += reloads_used * K;
  left = min(zombies, bullets);
  zombies -= left;
  bullets -= left;
  shots += left;
  if (zombies > 0) {
    return dp[index][last] = (1LL << 60);
  }
  mn = go(index + 1, last, bullets) + shots;
  if (reloads_used < delta || (index + 1 < N && L[index + 1] != R[index])) {
    mn = min(mn, go(index + 1, index + 1, K) + bullets + shots);
  }
  return dp[index][last] = mn;
}
void solve() {
  int i, j;
  long long results;
  scanf("%lld %lld", &N, &K);
  for (i = 0; i < N; i++) {
    scanf("%lld %lld %lld", &L[i], &R[i], &a[i]);
  }
  for (i = 0; i <= N; i++) {
    for (j = 0; j <= N; j++) {
      dp[i][j] = -1;
    }
  }
  results = go(0, 0, K);
  if (results >= (1LL << 60)) {
    printf("-1\n");
  } else {
    printf("%lld\n", results);
  }
}
int main() {
  solve();
  return 0;
}
