#include <bits/stdc++.h>
using namespace std;
long long divup(long long a, long long b) {
  if (a < 0) return 0;
  if (a % b == 0) return a / b;
  return a / b + 1;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<long long> l(n + 1), r(n + 1), a(n + 1);
  long long ans = 1e18;
  for (int i = 0; i < n; i++) {
    cin >> l[i] >> r[i] >> a[i];
  }
  vector<long long> dp(n + 1, 1e18);
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    long long rem = k, sum = dp[i - 1];
    for (int j = i; j <= n; j++) {
      long long need = divup(a[j - 1] - rem, k);
      if (r[j - 1] - l[j - 1] < need) break;
      rem = rem + need * k - a[j - 1];
      sum += a[j - 1];
      if (j < n) {
        if (l[j - 1] + need < l[j]) dp[j] = min(dp[j], sum + rem);
      } else
        ans = min(ans, sum);
    }
  }
  if (ans > 1e17)
    cout << -1 << "\n";
  else
    cout << ans << "\n";
  return 0;
}
