#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << ", ";
  err(++it, args...);
  cout << "\n";
}
long long powm(long long a, long long b, long long mod) {
  long long res = 1;
  while (b) {
    if (b & 1) res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
const long long mod = 1e9 + 7;
const long long N = 2005;
const long long inf = 4e17;
int n, k;
int l[N], r[N], a[N];
unordered_map<long long, long long> dp[N];
long long rec(long long pos, long long rem) {
  if (pos == n) return 0;
  long long tmp = a[pos] - (r[pos] - l[pos]) * 1ll * k - rem;
  if (tmp > 0) return inf;
  if (dp[pos].count(rem)) return dp[pos][rem];
  tmp = a[pos] - rem;
  long long m = (tmp + k - 1) / k;
  long long new_rem = m * k - tmp;
  long long ans = rec(pos + 1, new_rem);
  if (m < (l[pos + 1] - l[pos])) ans = min(ans, new_rem + rec(pos + 1, k));
  if (ans >= inf) ans = inf;
  return dp[pos][rem] = ans;
}
void solve() {
  cin >> n >> k;
  for (int i = 0; i < (n); i++) cin >> l[i] >> r[i] >> a[i];
  long long val = 0;
  for (int i = 0; i < (n); i++) val += a[i];
  cout << (rec(0, k) >= inf ? -1 : rec(0, k) + val) << '\n';
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cout << fixed << setprecision(15);
  int tc = 1;
  while (tc--) {
    solve();
  }
  return 0;
}
