#include <bits/stdc++.h>
using namespace std;
struct IO {
  IO() {
    ios::sync_with_stdio(0);
    cin.tie(0);
  }
} io;
using ll = long long;
using db = long double;
using pi = pair<ll, ll>;
using mi = map<ll, ll>;
const ll mod = 1e9 + 7;
const ll INF = 0x3f3f3f3f;
ll qpow(ll a, ll b, ll MOD = mod) {
  for (ll ans = 1;; a = a * a % MOD, b >>= 1) {
    if (b & 1) ans = ans * a % MOD;
    if (!b) return ans;
  }
}
ll inv(ll a, ll MOD = mod) { return qpow(a, MOD - 2, MOD); }
ll exgcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1, y = 0;
    return a;
  }
  ll ret = exgcd(b, a % b, y, x);
  y -= a / b * x;
  return ret;
}
ll getInv(ll a, ll mod) {
  ll x, y;
  ll d = exgcd(a, mod, x, y);
  return d == 1 ? (x % mod + mod) % mod : -1;
}
using vi = vector<ll>;
const ll N = 1e6 + 5, M = 1e9 + 7;
inline ll add(ll x, ll y) { return x + y >= M ? x + y - M : x + y; }
inline ll mul(ll x, ll y) { return x * y % M; }
inline ll sub(ll x, ll y) { return x - y < 0 ? x - y + M : x - y; }
using G = vector<vi>;
ll dp[N], l[N], r[N], a[N];
signed main() {
  ll n, k;
  cin >> n >> k;
  for (ll i = 1; i <= n; i++) cin >> l[i] >> r[i] >> a[i];
  for (ll i = n; i >= 1; i--) {
    ll need = dp[i + 1] * (r[i] == l[i + 1]) + a[i];
    if (need > k * (r[i] - l[i] + 1)) {
      cout << -1 << endl;
      return 0;
    }
    dp[i] = max(0ll, need - k * (r[i] - l[i]));
  }
  ll ans = 0;
  ll res = k;
  for (ll i = 1; i <= n; i++) {
    ans += a[i];
    if (dp[i] > res) {
      ans += res;
      res = k;
    }
    res = res - a[i] % k + k;
    if (res >= k) res -= k;
  }
  cout << ans << endl;
}
