#include <bits/stdc++.h>
using namespace std;
namespace Sakurajima_Mai {
const int infi = 2e9;
const long long infl = 4e18;
inline int qi() {
  int f = 0, fu = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') fu = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    f = (f << 3) + (f << 1) + c - 48;
    c = getchar();
  }
  return f * fu;
}
inline long long ql() {
  long long f = 0;
  int fu = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') fu = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    f = (f << 3) + (f << 1) + c - 48;
    c = getchar();
  }
  return f * fu;
}
inline double qd() {
  char c = getchar();
  int flag = 1;
  double ans = 0;
  while ((!(c >= '0' && c <= '9')) && c != '-') c = getchar();
  if (c == '-') flag = -1, c = getchar();
  while (c >= '0' && c <= '9') ans = ans * 10 + (c ^ 48), c = getchar();
  if (c == '.') {
    c = getchar();
    for (register int Bit = 10; c >= '0' && c <= '9';
         Bit = (Bit << 3) + (Bit << 1))
      ans += (double)(c ^ 48) * 1.0 / Bit, c = getchar();
  }
  return ans * flag;
}
inline long long ceil_div(long long a, long long b) { return (a + b - 1) / b; }
}  // namespace Sakurajima_Mai
using namespace Sakurajima_Mai;
const int MAX_N = 2e3 + 5;
int l[MAX_N], r[MAX_N], a[MAX_N];
long long dp[MAX_N];
int main() {
  int n = qi(), k = qi();
  for (register int i = 1, bbb = n; i <= bbb; ++i)
    l[i] = qi(), r[i] = qi(), a[i] = qi();
  for (register int i = n, bbb = 1; i >= bbb; --i) {
    long long need = a[i];
    if (r[i] == l[i + 1]) need += dp[i + 1];
    if (1ll * (r[i] - l[i] + 1) * k < need) {
      puts("-1");
      return 0;
    }
    dp[i] = max(0ll, need - 1ll * (r[i] - l[i]) * k);
  }
  int left = k;
  long long ans = 0;
  for (register int i = 1, bbb = n; i <= bbb; ++i) {
    if (left < dp[i]) ans += left, left = k;
    ans += a[i], left = ((left - a[i]) % k + k) % k;
  }
  printf("%lld\n", ans);
  return 0;
}
