#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  char ch = getchar();
  long long nega = 1;
  while (!isdigit(ch)) {
    if (ch == '-') nega = -1;
    ch = getchar();
  }
  long long ans = 0;
  while (isdigit(ch)) {
    ans = ans * 10 + ch - 48;
    ch = getchar();
  }
  if (nega == -1) return -ans;
  return ans;
}
long long n, k, a[2005];
long long l[2005], r[2005], v[2005];
vector<pair<long long, long long> > f, g;
signed main() {
  cin >> n >> k;
  for (long long i = 1; i <= n; i++)
    l[i] = read(), r[i] = read(), v[i] = read();
  f.emplace_back(k, 0);
  for (long long i = 1; i <= n; i++) {
    if (l[i] > r[i - 1]) {
      long long minn = 0x3f3f3f3f3f3f3f3f;
      for (auto [x, y] : f) {
        minn = min(minn, (x == k ? 0 : x) + y);
        if (x != k) g.emplace_back(x, y);
      }
      if (minn < 0x3f3f3f3f3f3f3f3f) g.emplace_back(k, minn);
      f = g;
      g.clear();
    }
    for (auto [x, y] : f) {
      long long A = y + v[i];
      long long ned = (max(0LL, v[i] - x) + k - 1) / k;
      if (l[i] + ned > r[i]) continue;
      long long B = ned * k - v[i] + x;
      g.emplace_back(B, A);
      if (l[i] + ned != r[i]) g.emplace_back(k, A + B);
    }
    f = g;
    g.clear();
  }
  long long ans = 0x3f3f3f3f3f3f3f3f;
  for (auto [x, y] : f) ans = min(ans, y);
  if (ans == 0x3f3f3f3f3f3f3f3f)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
