#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && (c != '-')) c = getchar();
  if (c == '-') f = -1, c = getchar();
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return x * f;
}
const int N = 2010;
const long long inf = 1e15;
int n, m, l[N], r[N], a[N];
long long f[N];
int main() {
  n = read(), m = read();
  for (register int i = (1); i <= (n); i++)
    l[i] = read(), r[i] = read(), a[i] = read();
  for (register int i = (1); i <= (n); i++) f[i] = inf;
  long long ans = 0;
  int j = 1, i = 0, flag = 0, s = m;
  while (j <= n) {
    if (s + 1ll * (r[j] - l[j]) * m < a[j]) {
      if (!flag) {
        ans = inf;
        break;
      }
      j = i + 1, ans = f[j - 1], flag = 0, s = m;
      if (s + 1ll * (r[j] - l[j]) * m < a[j]) {
        ans = inf;
        break;
      }
    }
    ans += a[j];
    if (s >= a[j]) {
      s -= a[j];
      if (l[j] != r[j] || r[j] < l[j + 1])
        f[j] = min(f[j], ans + s), i = j, flag = 1;
    } else {
      int y = a[j] - s, z = (y + m - 1) / m;
      s = z * m - y;
      if (r[j] - l[j] != z || r[j] < l[j + 1])
        f[j] = min(f[j], ans + s), i = j, flag = 1;
    }
    ++j;
  }
  if (ans == inf) ans = -1;
  printf("%lld\n", ans);
}
