#include <bits/stdc++.h>
#pragma GCC optimize(3)
#pragma GCC optimize(2)
#pragma GCC optimize("Ofast")
using namespace std;
const long long INF = 1e18;
vector<map<long long, long long>> DP;
long long l[2005], r[2005], a[2005];
int N, K;
template <class T>
void MIN(T& a, const T& b) {
  if (a > b) a = b;
}
int main() {
  scanf("%d%d", &N, &K);
  DP = vector<map<long long, long long>>(N + 2);
  for (int i = 1; i <= (int)N; ++i) {
    scanf("%lld%lld%lld", &l[i], &r[i], &a[i]);
  }
  DP[0][K] = 0;
  for (int i = 0; i <= (int)N; ++i) {
    for (auto& e : DP[i]) {
      long long j = e.first;
      long long dn = (a[i + 1] - j + K - 1) / K;
      if (l[i + 1] + dn <= r[i + 1]) {
        long long nj = j + dn * K - a[i + 1];
        if (!DP[i + 1].count(nj)) {
          DP[i + 1][nj] = INF;
        }
        MIN(DP[i + 1][nj], DP[i][j] + a[i + 1]);
        if (l[i + 1] + dn < r[i + 1] ||
            (l[i + 1] + dn == r[i + 1] && i != N && r[i + 1] < l[i + 2])) {
          if (!DP[i + 1].count(K)) {
            DP[i + 1][K] = INF;
          }
          MIN(DP[i + 1][K], DP[i][j] + dn * K + j);
        }
      }
    }
  }
  if (!DP[N + 1].size()) {
    printf("-1\n");
  } else {
    long long ret = INF;
    for (auto& e : DP[N + 1]) {
      MIN(ret, e.second);
    }
    printf("%lld\n", ret);
  }
}
