#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  for (int i = 0; i < (int)v.size(); i++) {
    cout << v[i] << (i == (int)v.size() - 1 ? '\n' : ' ');
  }
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& v) {
  for (auto& i : v) {
    cout << i << " ";
  }
  cout << endl;
  return os;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long len, k;
  cin >> len >> k;
  long long start[len], end[len], amount[len];
  for (int i = 0; i < len; i++) cin >> start[i] >> end[i] >> amount[i];
  long long needbegin[len];
  for (int i = len - 1; i >= 0; i--) {
    long long req = 0;
    if (i != len - 1 && end[i] == start[i + 1]) req += needbegin[i + 1];
    req += amount[i];
    if ((end[i] - start[i] + 1ll) * k < req) {
      cout << -1 << endl;
      return 0;
    }
    req -= (end[i] - start[i]) * k;
    req = max(req, 0ll);
    needbegin[i] = req;
  }
  long long used = 0, curamount = k;
  for (int i = 0; i < len; i++) {
    if (curamount < needbegin[i]) {
      used += curamount;
      curamount = k;
    }
    curamount = ((curamount - amount[i]) % k + k) % k;
    used += amount[i];
  }
  cout << used << endl;
}
