#include <bits/stdc++.h>
using namespace std;
constexpr long long INF = numeric_limits<long long>::max();
struct Case {
  long long currBullets = 0;
  long long usedBullets = 0;
  Case() = default;
  Case(long long curr, long long used) {
    currBullets = curr;
    usedBullets = used;
  }
};
struct Wave {
  int l;
  int r;
  int numM;
};
int main() {
  vector<Case> cases;
  int N, K;
  cin >> N >> K;
  vector<Wave> waves(N);
  for (auto& w : waves) {
    cin >> w.l >> w.r >> w.numM;
  }
  cases.emplace_back(K, 0);
  for (int wave = 0; wave < N; ++wave) {
    auto [l, r, numM] = waves[wave];
    Case reload(K, -INF);
    for (auto& c : cases) {
      int needT = int(numM - c.currBullets + K - 1) / K;
      if (l + needT > r) {
        c.currBullets = -1;
        continue;
      }
      c.currBullets = c.currBullets + needT * K - numM;
      c.usedBullets += numM;
      if ((size_t)wave + 1 < waves.size() &&
          l + needT < waves[(size_t)wave + 1].l &&
          (reload.usedBullets < 0 ||
           c.currBullets + c.usedBullets < reload.usedBullets)) {
        reload.usedBullets = c.currBullets + c.usedBullets;
      }
    }
    cases.erase(remove_if(cases.begin(), cases.end(),
                          [](const Case& c) { return c.currBullets < 0; }),
                cases.end());
    if (reload.usedBullets >= 0) {
      cases.push_back(reload);
    }
    if (cases.empty()) break;
  }
  long long result = INF;
  for (const auto& x : cases) {
    result = min(x.usedBullets, result);
  }
  cout << (result == INF ? -1 : result);
  return 0;
}
