#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, y = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') y = -y;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * y;
}
namespace qiqi {
const int N = 2005;
int n;
long long m, a[N], l[N], r[N], b[N];
void main() {
  n = read();
  m = read();
  for (int i = (1); i <= (n); ++i) {
    l[i] = read();
    r[i] = read();
    a[i] = read();
  }
  for (int i = (n); i >= (1); --i) {
    long long x = a[i] + (i < n && l[i + 1] == r[i] ? b[i + 1] : 0);
    if (x > (r[i] - l[i] + 1) * m) {
      puts("-1");
      return;
    }
    b[i] = max(0LL, x - (r[i] - l[i]) * m);
  }
  long long ans = 0, x = m;
  for (int i = (1); i <= (n); ++i) {
    if (x < b[i]) {
      ans += x;
      x = m;
    }
    ans += a[i];
    x = (x - a[i] % m + m) % m;
  }
  printf("%lld\n", ans);
}
}  // namespace qiqi
int main() {
  qiqi::main();
  return 0;
}
