#include <bits/stdc++.h>
using namespace std;
const int N = 114514;
long long l[N], r[N], a[N], f[N], nw, as, k;
int n;
int main() {
  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%lld%lld%lld", &l[i], &r[i], &a[i]);
  for (int i = n; i >= 1; i--) {
    nw = a[i] + (r[i] == l[i + 1] ? f[i + 1] : 0);
    if ((r[i] - l[i] + 1) * k < nw) {
      puts("-1");
      exit(0);
    }
    f[i] = max(0ll, nw - (r[i] - l[i]) * k);
  }
  long long ct = k;
  for (int i = 1; i <= n; i++) {
    if (ct < f[i]) as += ct, ct = k;
    as += a[i];
    ct = ((ct - a[i]) % k + k) % k;
  }
  printf("%lld", as);
}
