#include <bits/stdc++.h>
inline long long read() {
  long long x = 0;
  bool f = 0;
  char c = getchar();
  while (!isdigit(c)) f |= (c == '-'), c = getchar();
  while (isdigit(c)) x = x * 10 + (c & 15), c = getchar();
  return f ? -x : x;
}
namespace pb_ds {
namespace io {
const long long MaxBuff = 1 << 15;
const long long Output = 1 << 23;
char B[MaxBuff], *S = B, *T = B;
char Out[Output], *iter = Out;
inline void flush() {
  fwrite(Out, 1, iter - Out, stdout);
  iter = Out;
}
}  // namespace io
template <class Type>
inline Type Read() {
  using namespace io;
  register char ch;
  register Type ans = 0;
  register bool neg = 0;
  while (ch = ((S == T) && (T = (S = B) + fread(B, 1, MaxBuff, stdin), S == T)
                   ? 0
                   : *S++),
         (ch < '0' || ch > '9') && ch != '-')
    ;
  ch == '-' ? neg = 1 : ans = ch - '0';
  while (ch = ((S == T) && (T = (S = B) + fread(B, 1, MaxBuff, stdin), S == T)
                   ? 0
                   : *S++),
         '0' <= ch && ch <= '9')
    ans = ans * 10 + ch - '0';
  return neg ? -ans : ans;
}
template <class Type>
inline void Print(register Type x, register char ch = '\n') {
  using namespace io;
  if (!x)
    *iter++ = '0';
  else {
    if (x < 0) *iter++ = '-', x = -x;
    static long long s[100];
    register long long t = 0;
    while (x) s[++t] = x % 10, x /= 10;
    while (t) *iter++ = '0' + s[t--];
  }
  *iter++ = ch;
}
}  // namespace pb_ds
using namespace pb_ds;
using namespace std;
long long n, k, l[2005], r[2005], a[2005], dp[2005];
signed main() {
  n = Read<long long>(), k = Read<long long>();
  for (register long long i = 1; i <= n; ++i)
    l[i] = Read<long long>(), r[i] = Read<long long>(),
    a[i] = Read<long long>();
  for (register long long i = n; i >= 1; --i) {
    long long need = a[i] + (i < n && r[i] == l[i + 1] ? dp[i + 1] : 0);
    if ((r[i] - l[i] + 1) * k < need) {
      puts("-1");
      return 0;
    }
    dp[i] = need - (r[i] - l[i]) * k;
    if (dp[i] < 0) dp[i] = 0;
  }
  long long ans = 0, cur = k;
  for (register 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;
  }
  printf("%lld", ans);
  return 0;
}
