#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, K;
  cin >> n >> K;
  long long int dp[n + 1][n + 1], now[n + 1][n + 1];
  long long int l[n + 1], r[n + 1], a[n + 1];
  long long int f[n + 1][n + 1];
  long long int ok[n + 1][n + 1];
  for (int i = 0; i < (n + 1); ++i) {
    for (int j = 0; j < (n + 1); ++j) {
      dp[i][j] = 0;
      now[i][j] = 0;
      f[i][j] = 0;
      ok[i][j] = 0;
    }
    l[i] = 0;
    r[i] = 0;
    a[i] = 0;
  }
  for (int i = 1; i < n + 1; ++i) {
    cin >> l[i] >> r[i] >> a[i];
  }
  dp[0][0] = 0;
  now[0][0] = K;
  f[0][0] = 0;
  ok[0][0] = 1;
  for (int i = 1; i < n + 1; ++i) {
    for (int j = 0; j < (i); ++j) {
      if (ok[i - 1][j] == 0) {
        ok[i][j] = 0;
        continue;
      }
      ok[i][j] = 1;
      f[i][j] = 0;
      dp[i][j] = dp[i - 1][j];
      long long int tmp = now[i - 1][j];
      if (tmp >= a[i]) {
        dp[i][j] += a[i];
        now[i][j] = tmp - a[i];
        if (r[i] > l[i]) f[i][j] = 1;
      } else {
        long long int aa = a[i] - tmp;
        dp[i][j] += tmp;
        tmp = 0;
        long long int need = (aa - 1) / K + 1;
        if (r[i] - l[i] < need) {
          ok[i][j] = 0;
        } else if (r[i] - l[i] == need) {
          dp[i][j] += aa;
          now[i][j] = K - (aa % K);
          if (now[i][j] == K) now[i][j] = 0;
        } else {
          dp[i][j] += aa;
          now[i][j] = K - (aa % K);
          if (now[i][j] == K) now[i][j] = 0;
          f[i][j] = 1;
        }
      }
      if (i < n && r[i] < l[i + 1]) f[i][j] = 1;
    }
    dp[i][i] = 1001001001001001001;
    now[i][i] = 0;
    f[i][i] = 0;
    ok[i][i] = 0;
    for (int j = 0; j < (i); ++j) {
      if (ok[i - 1][j] == 0) continue;
      long long int dpp = dp[i - 1][j];
      if (f[i - 1][j] == 0 && now[i - 1][j] != K) continue;
      if (now[i - 1][j] != K) {
        dpp += now[i - 1][j];
      }
      long long int tmp = K;
      ok[i][i] = 1;
      if (tmp >= a[i]) {
        dpp += a[i];
        tmp = tmp - a[i];
        now[i][i] = tmp;
        if (r[i] > l[i]) f[i][i] = 1;
      } else {
        long long int aa = a[i] - tmp;
        dpp += tmp;
        tmp = 0;
        long long int need = (aa - 1) / K + 1;
        if (r[i] - l[i] < need) {
          ok[i][i] = 0;
        } else if (r[i] - l[i] == need) {
          dpp += aa;
          now[i][i] = K - (aa % K);
          if (now[i][i] == K) now[i][i] = 0;
        } else {
          dpp += aa;
          now[i][i] = K - (aa % K);
          f[i][i] = 1;
          if (now[i][i] == K) now[i][i] = 0;
        }
      }
      if (ok[i][i]) {
        dp[i][i] = min(dp[i][i], dpp);
        if (i < n && r[i] < l[i + 1]) f[i][i] = 1;
      }
    }
  }
  long long int ans = 1001001001001001001;
  for (int i = 0; i < (n + 1); ++i) {
    if (ok[n][i]) {
      ans = min(ans, dp[n][i]);
    }
  }
  if (ans == 1001001001001001001) ans = -1;
  cout << ans << endl;
  return 0;
}
