#include <bits/stdc++.h>
using namespace std;
int n;
int l[2005], r[2005];
long long k, w[2005];
long long s[2005][2005];
long long f[2005];
long long h[2005];
long long up(long long x) { return (x / k + (x % k > 0)); }
int main() {
  cin >> n >> k;
  for (int i = (1); i <= (n); i++) cin >> l[i] >> r[i] >> w[i];
  for (int i = (1); i <= (n); i++) h[i] = -1;
  for (int i = (1); i <= (n); i++)
    for (int j = (1); j <= (n); j++) s[i][j] = -1;
  for (int i = (1); i <= (n); i++) {
    long long start = k;
    long long has = 0;
    for (int j = (i); j <= (n); j++) {
      if (start >= w[j]) {
        if (l[j] < l[j + 1]) s[i][j] = has + start;
        has += w[j];
        start -= w[j];
        if (j == n) h[i] = has;
      } else {
        long long t = up(w[j] - start) + 1;
        if (t > r[j] - l[j] + 1) break;
        if (l[j] + t - 1 < l[j + 1]) s[i][j] = has + (t - 1) * k + start;
        has += w[j];
        if (j == n) h[i] = has;
        start = start + (t - 1) * k - w[j];
      }
    }
  }
  for (int i = (1); i <= (n - 1); i++) {
    f[i] = -1;
    for (int j = (i - 1); j >= (0); j--) {
      if (s[j + 1][i] >= 0 && f[j] >= 0)
        if (f[i] < 0 || f[j] + s[j + 1][i] < f[i]) f[i] = f[j] + s[j + 1][i];
    }
  }
  long long ans = -1;
  for (int i = (0); i <= (n - 1); i++)
    if (f[i] >= 0 && h[i + 1] >= 0) {
      if (ans < 0 || f[i] + h[i + 1] < ans) ans = f[i] + h[i + 1];
    }
  cout << ans << endl;
  return 0;
}
