#include <bits/stdc++.h>
using namespace std;
const long long N = 2e3 + 5;
const long long mod = 1e9 + 7;
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;
long long n, k, a[N], l[N], r[N], dp[N][N][2];
long long mem(int i, int j, int first, long long left) {
  if (i == n) return 0;
  if (dp[i][j][first] != -1) return dp[i][j][first];
  dp[i][j][first] = INF;
  long long d = r[i] - l[i] + 1 - first;
  if (left >= a[i]) {
    if (d == 0) return INF;
    if (d == 1) {
      dp[i][j][first] = min(dp[i][j][first], mem(i + 1, j, 0, left - a[i]));
      dp[i][j][first] = min(dp[i][j][first],
                            mem(i + 1, i, (l[i + 1] == r[i]), k) + left - a[i]);
    } else {
      dp[i][j][first] = min(dp[i][j][first], mem(i + 1, j, 0, left - a[i]));
      dp[i][j][first] = min(dp[i][j][first], mem(i + 1, i, 0, k) + left - a[i]);
    }
  } else {
    int t = ceil((long double)(a[i] - left) / k) + 1;
    if (t > d) return INF;
    long long nl = (k - (a[i] - left) % k) % k;
    if (t == d) {
      dp[i][j][first] = min(dp[i][j][first], mem(i + 1, j, 0, nl));
      dp[i][j][first] =
          min(dp[i][j][first], mem(i + 1, i, (l[i + 1] == r[i]), k) + nl);
    } else {
      dp[i][j][first] = min(dp[i][j][first], mem(i + 1, j, 0, nl));
      dp[i][j][first] = min(dp[i][j][first], mem(i + 1, i, 0, k) + nl);
    }
  }
  return dp[i][j][first];
}
void solve() {
  memset(dp, -1, sizeof(dp));
  cin >> n >> k;
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> l[i] >> r[i] >> a[i];
    sum += a[i];
  }
  long long x = mem(0, n, 0, k);
  if (x >= INF)
    cout << -1 << "\n";
  else
    cout << x + sum << "\n";
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed;
  cout << setprecision(15);
  ;
  int test = 1;
  for (int i = 1; i <= test; i++) {
    solve();
  }
}
