#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18, N = 5000;
int l[N], r[N], w[N];
int n, K;
long long f[N];
int main() {
  scanf("%d%d", &n, &K);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d%d", l + i, r + i, w + i);
    if (1ll * (r[i] - l[i] + 1) * K < w[i]) {
      puts("-1");
      return 0;
    }
  }
  l[n + 1] = r[n] + 1;
  for (int i = 1; i <= n; i++) f[i] = INF;
  for (int i = n; i >= 1; i--) {
    int res = K, j;
    long long presum = 0;
    for (j = i + 1; j <= n + 1; j++) {
      if (res + 1ll * (r[j - 1] - l[j - 1]) * K < w[j - 1]) break;
      int t = max(0ll, (0ll + w[j - 1] - res + K - 1) / K);
      res = (1ll * t * K + res) - w[j - 1];
      presum += w[j - 1];
      if (j > n) res = 0;
      if (l[j] != r[j - 1] || t < r[j - 1] - l[j - 1])
        f[i] = min(f[i], res + presum + f[j]);
    }
  }
  if (f[1] == INF)
    puts("-1");
  else
    cout << f[1] << endl;
}
