#include <bits/stdc++.h>
using namespace std;
const int maxn = 2005;
long long dp[maxn];
int l[maxn], r[maxn], a[maxn], k, n;
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i)
    scanf("%d%d%d", &l[i], &r[i], &a[i]), dp[i] = 1e18;
  dp[0] = 0;
  for (int i = 0; i < n; ++i) {
    long long ans = dp[i], num = k;
    for (int j = i + 1; j <= n; ++j) {
      long long time = (max(0ll, (a[j] - num)) + k - 1) / k;
      if (l[j] + time > r[j]) break;
      long long cost = num + time * k - a[j];
      ans += a[j];
      if (j == n)
        dp[j] = min(dp[j], ans);
      else if (l[j] + time < l[j + 1])
        dp[j] = min(dp[j], ans + cost);
      num = cost;
    }
  }
  cout << (dp[n] == 1e18 ? -1 : dp[n]) << "\n";
  return 0;
}
