#include <bits/stdc++.h>
using namespace std;
const int N = 2008;
const long long INF = 1e18;
int n, k;
long long a[N], L[N], R[N];
long long dp[N][N];
long long solve(int cur, int last, long long have) {
  if (cur >= n) {
    return 0;
  }
  long long& r = dp[cur][last];
  if (r != -1) return r;
  r = INF;
  if (have >= a[cur]) {
    r = min(r, a[cur] + solve(cur + 1, last, have - a[cur]));
    if (R[cur] > L[cur] || L[cur + 1] > R[cur]) {
      r = min(r, have + solve(cur + 1, cur + 1, k));
    }
  } else {
    long long rem = a[cur] - have;
    long long x = (rem + k - 1) / k;
    long long w = R[cur] - L[cur];
    if (x <= w) {
      r = min(r, a[cur] + solve(cur + 1, last, x * k - rem));
      if (x < w || L[cur + 1] > R[cur]) {
        r = min(r, have + x * k + solve(cur + 1, cur + 1, k));
      }
    }
  }
  return r;
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> L[i] >> R[i] >> a[i];
  }
  memset(dp, -1, sizeof dp);
  L[n] = INF;
  long long ans = solve(0, 0, k);
  if (ans >= INF) ans = -1;
  cout << ans << '\n';
  return 0;
}
