#include <bits/stdc++.h>
using namespace std;
const long long inf = INT_MAX, df = 2005;
long long i, j, k, l[df], m, n, o, p, q, r[df], s, t, u, v, w, x, y, z, a[df],
    dp[df], ans;
inline long long read() {
  long long x = 0, y = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') y = (ch == '-') ? -1 : 1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return x * y;
}
int main() {
  n = read(), k = read();
  for (long long i = (1); i <= (n); ++i)
    l[i] = read(), r[i] = read(), a[i] = read();
  for (long long i = (n); i >= (1); --i) {
    long long tmp = a[i] + ((i < n && r[i] == l[i + 1]) ? dp[i + 1] : 0);
    if ((r[i] - l[i] + 1) * k < tmp) return puts("-1"), 0;
    dp[i] = tmp - (r[i] - l[i]) * k;
    if (dp[i] < 0) dp[i] = 0;
  }
  long long cur = k;
  for (long long i = (1); i <= (n); ++i) {
    if (cur < dp[i]) {
      ans += cur;
      cur = k;
    }
    ans += a[i];
    cur = ((cur - a[i]) % k + k) % k;
  }
  return printf("%lld\n", ans), 0;
}
