#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, y = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') y = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  return x * y;
}
const int N = 2005;
int n, k;
struct node {
  int l, r, num;
} p[N];
inline void calc_time(int mon, int &tm, int &bul) {
  if (mon <= bul) {
    tm = 0;
    bul -= mon;
    return;
  }
  mon -= bul;
  tm = mon / k;
  if (tm * k < mon) {
    tm++;
    bul = tm * k - mon;
  } else
    bul = 0;
}
void dfs(int w, int bul, int time, long long tot) {
  while (w <= n) {
    int tm;
    calc_time(p[w].num, tm, bul);
    tot += p[w].num;
    if (tm + time > p[w].r) return;
    time += tm;
    if (w == n) {
      printf("%lld\n", tot);
      exit(0);
    }
    if (p[w + 1].l <= time)
      w++;
    else {
      dfs(w + 1, bul, p[w + 1].l, tot);
      dfs(w + 1, k, p[w + 1].l, tot + bul);
      puts("-1");
      exit(0);
    }
  }
}
int main() {
  n = read();
  k = read();
  for (int i = 1; i <= n; i++) {
    p[i].l = read();
    p[i].r = read();
    p[i].num = read();
  }
  dfs(1, k, p[1].l, 0);
  puts("-1");
  return 0;
}
