#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const int mod = 1e9 + 7;
const int N = 2e3 + 10;
const int inf = 1e9;
int n, k;
int l[N], r[N], a[N];
long long dp[N];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d%d", &l[i], &r[i], &a[i]);
  }
  for (int i = n; i >= 1; i--) {
    long long res = a[i];
    if (r[i] == l[i + 1]) res += dp[i + 1];
    if (1ll * k * (r[i] - l[i] + 1) < res) {
      puts("-1");
      return 0;
    }
    dp[i] = max(0ll, res - 1ll * k * (r[i] - l[i]));
  }
  long long ans = 0, res = k;
  for (int i = 1; i <= n; i++) {
    ans += a[i];
    if (a[i] < res) {
      res -= a[i];
    } else {
      a[i] -= res;
      res = (k - a[i] % k) % k;
    }
    if (res < dp[i + 1]) {
      ans += res;
      res = k;
    }
  }
  printf("%lld\n", ans);
  return 0;
}
