#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int M = 2000 + 7;
struct node {
  long long l, r, a;
} p[M];
long long dp[M];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> p[i].l >> p[i].r >> p[i].a;
  }
  bool flag = true;
  for (int i = n; i >= 1; i--) {
    if (p[i].r == p[i + 1].l)
      dp[i] = max(0ll, p[i].a + dp[i + 1] - (p[i].r - p[i].l) * k);
    else
      dp[i] = max(0ll, p[i].a - (p[i].r - p[i].l) * k);
    if (dp[i] > k) flag = false;
  }
  if (!flag) {
    cout << -1 << endl;
    return 0;
  }
  long long ans = 0, tp = k;
  for (int i = 1; i <= n; i++) {
    if (tp < dp[i]) ans += tp, tp = k;
    ans += p[i].a;
    if (p[i].a >= tp)
      p[i].a -= tp, tp = k - p[i].a % k;
    else
      tp -= p[i].a;
  }
  cout << ans << endl;
  return 0;
}
