#include <bits/stdc++.h>
using namespace std;
const int N = 2005;
int n;
long long dp[N], a[N], ans, k, l[N], r[N];
int main() {
  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; ++i) scanf("%lld%lld%lld", l + i, r + i, a + i);
  int flag = 0;
  for (int i = n; i >= 1; --i) {
    if (r[i] == l[i + 1])
      dp[i] = max((long long)0, a[i] + dp[i + 1] - (r[i] - l[i]) * k);
    else
      dp[i] = max((long long)0, a[i] - (r[i] - l[i]) * k);
    if (dp[i] > k) {
      flag = 1;
      break;
    }
  }
  if (flag) {
    printf("-1\n");
    return 0;
  }
  long long tmp = k;
  for (int i = 1; i <= n; ++i) {
    if (tmp < dp[i]) ans += tmp, tmp = k;
    ans += a[i];
    if (a[i] >= tmp)
      a[i] -= tmp, tmp = k - a[i] % k;
    else
      tmp -= a[i];
  }
  printf("%lld\n", ans);
  return 0;
}
