#include <bits/stdc++.h>
using namespace std;
long long dp[2010];
pair<int, int> l[2010];
int qtd[2010];
int N, G;
long long solve(int n) {
  if (n == N) return 0;
  if (dp[n] != -1) return dp[n];
  dp[n] = 1000000000000000009;
  long long disponivel = G;
  for (int i = n; i < N; i++) {
    long long m = qtd[i];
    long long agora = min(disponivel, m);
    disponivel -= agora;
    m -= agora;
    long long gasto = m / G + (m % G > 0);
    long long sobra = disponivel;
    if (m % G) sobra = G - (m % G);
    if (l[i].first + gasto > l[i].second) break;
    if (i == N - 1 and l[i].first + gasto <= l[i].second) {
      dp[n] = 0;
      break;
    }
    if (l[i].first + gasto < l[i + 1].first) {
      dp[n] = min(dp[n], solve(i + 1) + sobra);
    }
    if (m > 0) disponivel = sobra;
  }
  return dp[n];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  memset(dp, -1, sizeof(dp));
  cin >> N >> G;
  long long res = 0;
  for (int i = 0; i < N; i++) {
    int x, y, a;
    cin >> x >> y >> a;
    l[i] = {x, y};
    qtd[i] = a;
    res += a;
  }
  if (solve(0) < 1000000000000000009)
    cout << solve(0) + res << endl;
  else
    cout << -1 << endl;
}
