#include <bits/stdc++.h>
using namespace std;
long long n, k, l[2005], r[2005], a[2005], dp[2005], sum, ans;
long long f(int pos) {
  if (pos == n) return 0;
  if (dp[pos] != -1) return dp[pos];
  long long res = LLONG_MAX, curr = k, tmp, mini;
  bool flag = true;
  for (int i = pos; i < n; i++) {
    tmp = a[i];
    mini = min(curr, tmp);
    tmp -= mini;
    curr -= mini;
    if (tmp == 0) {
      if (l[i + 1] > l[i]) {
        if (f(i + 1) != LLONG_MAX) res = min(res, curr + f(i + 1));
      }
    } else {
      if (l[i] + (tmp / k) + int(tmp % k != 0) <= r[i]) {
        if (l[i] + (tmp / k) + int(tmp % k != 0) < l[i + 1]) {
          if (f(i + 1) != LLONG_MAX) {
            res = min(res, (k - (tmp % k)) % k + f(i + 1));
          }
        }
        curr = (k - (tmp % k)) % k;
      } else {
        flag = false;
        break;
      }
    }
  }
  if (flag) res = 0;
  return dp[pos] = res;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  memset(dp, -1, sizeof(dp));
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> l[i] >> r[i] >> a[i];
    sum += a[i];
  }
  l[n] = 1e9 + 5;
  ans = f(0);
  if (ans == LLONG_MAX) {
    cout << "-1\n";
  } else
    cout << sum + ans << "\n";
  return 0;
}
