#include <bits/stdc++.h>
using namespace std;
const int N = 2e3 + 5;
typedef int arr[N];
int n, k;
arr l, r, a, f;
inline void Solve() {
  for (int i = 1, iend = (n) + 1; i < iend; ++i)
    scanf("%d%d%d", l + i, r + i, a + i);
  for (int i = n, iend = (1) - 1; i > iend; --i) {
    int Need = a[i];
    if (i < n && r[i] == l[i + 1]) Need += f[i + 1];
    if (Need > (long long)(r[i] - l[i] + 1) * k) return void(puts("-1"));
    f[i] = max(0ll, Need - (long long)(r[i] - l[i]) * k);
  }
  long long Ans = accumulate(a + 1, a + n + 1, 0ll), Cur = k;
  for (int i = 1, iend = (n) + 1; i < iend; ++i) {
    if (Cur < f[i]) Ans += Cur, Cur = k;
    Cur = ((Cur - a[i]) % k + k) % k;
  }
  printf("%lld\n", Ans);
}
int main() {
  while (~scanf("%d%d", &n, &k)) Solve();
  return 0;
}
