#include <bits/stdc++.h>
using namespace std;
long long N, K, L[2005], R[2005], A[2005], dp[2005];
inline void rem(int i, long long &r, long long &d) {
  if (A[i] <= r) {
    r -= A[i], d = 0;
  } else {
    d = (A[i] - r + K - 1) / K, r = d * K - (A[i] - r);
  }
}
inline bool judge(int i, long long r) { return A[i] <= (R[i] - L[i]) * K + r; }
int main() {
  scanf("%lld%lld", &N, &K);
  long long s = 0;
  for (int i = 0; i < N; i++) {
    scanf("%lld%lld%lld", L + i, R + i, A + i);
    s += A[i];
  }
  fill(dp, dp + N, LLONG_MAX);
  dp[0] = 0;
  long long res = LLONG_MAX;
  for (int i = 0; i < N; i++) {
    long long r = K, d, tmp = dp[i];
    for (int j = i; j < N; j++) {
      if (tmp == LLONG_MAX || !judge(j, r)) {
        break;
      } else {
        rem(j, r, d);
        if (j + 1 < N) {
          if (d < R[j] - L[j] || R[j] < L[j + 1]) {
            dp[j + 1] = min(dp[j + 1], tmp + r);
          }
        } else {
          res = min(res, tmp);
        }
      }
    }
  }
  printf("%lld\n", res == LLONG_MAX ? -1 : res + s);
  return 0;
}
