#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define int long long
#define maxn (2020)
#define inf (10000000000000ll)
int l[maxn], r[maxn], a[maxn], d[maxn];
signed main()
{
	ios::sync_with_stdio(false);

	int n, k; cin >> n >> k;
	for(int i = 1; i <= n; i++) {
		cin >> l[i] >> r[i] >> a[i];
	}
	for(int i = 1; i <= n; i++) d[i] = inf;
	int ok = 1;
	for(int i = n; i >= 1; i--) {
		int total = (r[i] - l[i] + 1) * k;
		if(total < a[i]) {ok = 0; break;}
		int remain = (total - a[i]) % k;
		int reload = (int)(total - a[i] >= k || l[i + 1] - r[i] > 0);
		int sum = a[i], over = 1;
		for(int j = i + 1; j <= n; j++) {
			if(reload) d[i] = min(d[i], sum + remain + d[j]);
			int total = remain + (r[j] - l[j]) * k;
			if(total < a[j]) {over = 0; break;}
			remain = (total - a[j]) % k;
			reload = (int)(total - a[j] >= k || l[j + 1] - r[j] > 0);
			sum += a[j];
		}
		if(over) d[i] = min(d[i], sum);
		if(d[i] >= inf) {ok = 0; break;}
	}
	if(d[1] < inf)
		cout << d[1] << endl;
	else
		cout << -1 << endl;

	return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define MAXN 2010
#define INF 100000000000000
using namespace std;
template<typename T> void Read(T &cn)
{
	char c; int sig = 1;
	while(!isdigit(c = getchar())) if(c == '-') sig = 0;
	if(sig) {cn = c-48; while(isdigit(c = getchar())) cn = cn*10+c-48; }
	else    {cn = 48-c; while(isdigit(c = getchar())) cn = cn*10+48-c; }
}
template<typename T> void Write(T cn)
{
	int wei = 0; T cm = 0; int cx = cn%10; cn/=10;
	if(cn < 0 || cx < 0) {putchar('-'); cn = 0-cn; cx = 0-cx; }
	while(cn)cm = cm*10+cn%10,cn/=10,wei++;
	while(wei--)putchar(cm%10+48),cm/=10;
	putchar(cx+48);
}
template<typename T> void WriteL(T cn) {Write(cn); puts(""); }
template<typename T> void WriteS(T cn) {Write(cn); putchar(' '); }
template<typename T> void Max(T &cn, T cm) {cn = cn < cm ? cm : cn; }
template<typename T> void Min(T &cn, T cm) {cn = cn < cm ? cn : cm; }
int n;
LL k;
int a[MAXN+1], l[MAXN+1], r[MAXN+1];
LL f[MAXN+1];
int main()
{
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	Read(n); Read(k);
	for(int i = 1;i<=n;i++) Read(l[i]), Read(r[i]), Read(a[i]);
	for(int i = 1;i<=n;i++) f[i] = INF; f[0] = 0;
	for(int i = 0;i<=n-1;i++)
	{
//		printf("f[%d] = %lld\n",i,f[i]);
		int lef_bu = k, lef_mo = 0;
		for(int j = i+1;j<=n;j++)
		{
			if(lef_bu+k*(r[j]-l[j]) < lef_mo+a[j]) break;
			if(j == n) {Min(f[j], f[i]); break; }
			int br = min(l[j+1]-1, r[j]);
			if(lef_bu+k*(br-l[j]) >= lef_mo+a[j]) {
				lef_bu = (lef_bu+k*(br-l[j])-lef_mo-a[j])%k, lef_mo = 0;
				if(lef_bu == 0) lef_bu = k;
				Min(f[j], f[i]+lef_bu);
			}
			else {
				if(r[j] == l[j]) lef_mo = lef_mo+a[j];
				else lef_mo = lef_mo+a[j]-lef_bu-k*(br-l[j]), lef_bu = k;
			}
//			printf("  j = %d lef_mo = %d lef_bu = %d\n",j,lef_mo,lef_bu);
		}
	}
	if(f[n] >= INF) {puts("-1"); return 0; }
	for(int i = 1;i<=n;i++) f[n] = f[n]+a[i];
	WriteL(f[n]);
	return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define MAXN 2010
#define INF 100000000000000
using namespace std;
template<typename T> void Read(T &cn)
{
	char c; int sig = 1;
	while(!isdigit(c = getchar())) if(c == '-') sig = 0;
	if(sig) {cn = c-48; while(isdigit(c = getchar())) cn = cn*10+c-48; }
	else    {cn = 48-c; while(isdigit(c = getchar())) cn = cn*10+48-c; }
}
template<typename T> void Write(T cn)
{
	int wei = 0; T cm = 0; int cx = cn%10; cn/=10;
	if(cn < 0 || cx < 0) {putchar('-'); cn = 0-cn; cx = 0-cx; }
	while(cn)cm = cm*10+cn%10,cn/=10,wei++;
	while(wei--)putchar(cm%10+48),cm/=10;
	putchar(cx+48);
}
template<typename T> void WriteL(T cn) {Write(cn); puts(""); }
template<typename T> void WriteS(T cn) {Write(cn); putchar(' '); }
template<typename T> void Max(T &cn, T cm) {cn = cn < cm ? cm : cn; }
template<typename T> void Min(T &cn, T cm) {cn = cn < cm ? cn : cm; }
int n;
LL k;
int a[MAXN+1], l[MAXN+1], r[MAXN+1];
LL f[MAXN+1];
int main()
{
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	Read(n); Read(k);
	for(int i = 1;i<=n;i++) Read(l[i]), Read(r[i]), Read(a[i]);
	for(int i = 1;i<=n;i++) f[i] = INF; f[0] = 0;
	for(int i = 0;i<=n-1;i++)
	{
//		printf("f[%d] = %lld\n",i,f[i]);
		int lef_bu = k, lef_mo = 0;
		for(int j = i+1;j<=n;j++)
		{
			if(lef_bu+k*(r[j]-l[j]) < lef_mo+a[j]) break;
			if(j == n) {Min(f[j], f[i]); break; }
			int br = min(l[j+1]-1, r[j]);
			if(lef_bu+k*(br-l[j]) >= lef_mo+a[j]) {
				lef_bu = (lef_bu+k*(br-l[j])-lef_mo-a[j])%k, lef_mo = 0;
				if(lef_bu == 0) lef_bu = k;
				Min(f[j], f[i]+lef_bu);
			}
			else {
				if(r[j] == l[j]) lef_mo = lef_mo+a[j];
				else lef_bu = k, lef_mo = lef_mo+a[j]-lef_bu-k*(r[j]-l[j]-1);
			}
//			printf("  j = %d lef_mo = %d lef_bu = %d\n",j,lef_mo,lef_bu);
		}
	}
	if(f[n] >= INF) {puts("-1"); return 0; }
	for(int i = 1;i<=n;i++) f[n] = f[n]+a[i];
	WriteL(f[n]);
	return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define MAXN 2010
#define INF 100000000000000
using namespace std;
template<typename T> void Read(T &cn)
{
	char c; int sig = 1;
	while(!isdigit(c = getchar())) if(c == '-') sig = 0;
	if(sig) {cn = c-48; while(isdigit(c = getchar())) cn = cn*10+c-48; }
	else    {cn = 48-c; while(isdigit(c = getchar())) cn = cn*10+48-c; }
}
template<typename T> void Write(T cn)
{
	int wei = 0; T cm = 0; int cx = cn%10; cn/=10;
	if(cn < 0 || cx < 0) {putchar('-'); cn = 0-cn; cx = 0-cx; }
	while(cn)cm = cm*10+cn%10,cn/=10,wei++;
	while(wei--)putchar(cm%10+48),cm/=10;
	putchar(cx+48);
}
template<typename T> void WriteL(T cn) {Write(cn); puts(""); }
template<typename T> void WriteS(T cn) {Write(cn); putchar(' '); }
template<typename T> void Max(T &cn, T cm) {cn = cn < cm ? cm : cn; }
template<typename T> void Min(T &cn, T cm) {cn = cn < cm ? cn : cm; }
int n;
LL k;
int a[MAXN+1], l[MAXN+1], r[MAXN+1];
LL b[MAXN+1], zuo[MAXN+1], you[MAXN+1], blen;
LL f[MAXN+1];
int main()
{
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	Read(n); Read(k);
	for(int i = 1;i<=n;i++) Read(l[i]), Read(r[i]), Read(a[i]);
	blen = 0;
	for(int i = 1;i<=n;)
	{
		int j = i;
		while(j < n && l[j+1] == r[j]) j++;
		LL zong = 0;
		for(int ij = i;ij<=j;ij++)
		{
			zong = zong + a[ij];
//			printf("zong = %lld (%d,%d)\n",zong,r[ij],l[i]);
			if(1ll*(r[ij]-l[i]+1)*k < zong) {puts("-1"); return 0; }
		}
		++blen; b[blen] = zong; zuo[blen] = l[i]; you[blen] = r[j]; i = j+1;
	}
	for(int i = 1;i<=n;i++) f[i] = INF; f[0] = 0;
	for(int i = 0;i<=n-1;i++)
	{
//		printf("f[%d] = %lld\n",i,f[i]);
		int lef = k;
		for(int j = i+1;j<=n;j++)
		{
			if(b[j] <= lef+k*(you[j]-zuo[j])) lef = k-(b[j]-lef)%k;
			else break;
			if(j == n) lef = 0;
			Min(f[j], f[i]+lef);
		}
	}
	for(int i = 1;i<=n;i++) f[n] = f[n]+b[i];
	WriteL(f[n]);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int MAXN=2005;

int a[MAXN][3];
int f[MAXN];
int main(){
	int n,k; scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++){
		scanf("%d%d%d",&a[i][0],&a[i][1],&a[i][2]);
		if(a[i-1][1]==a[i][0]){
			a[i][0]=a[i-1][0]; a[i][2]+=a[i-1][2];
			a[i-1][0]=a[i-1][1]=a[i-1][2]=-1;}
	}
	for(int i=1;i<=n;i++)
		f[i]=max(0,a[i][2]-(a[i][1]-a[i][0])*k);

	long long ans=k,num=k;
	bool vis=false;
	for(int i=1;i<=n;i++){
		if(a[i][0]==-1) continue;
		if(num<f[i]){vis=true; break;}

		ans+=max(0,(int)ceil((1.0*a[i][2]-num)/k))*k;
		num=max(0,(int)ceil((1.0*a[i][2]-num)/k))*k+num-a[i][2];
		if(i==n) break;

		while(a[i+1][0]==-1) i++;
		if(num<a[i+1][2]) num=k,ans+=k;
	}
	ans-=num;

	if(vis) printf("-1\n");
	else printf("%lld\n",ans);

	return 0;
}


#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#include <bits/stdc++.h>
#define N 100005
#define int long long
using namespace std;

template <typename T>

void read(T &a)
{
	T x = 0,f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9')
	{
		if (ch == '-') f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9')
	{
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	a = x * f;
}

template <typename T,typename... Args> void read(T &t,Args &... args) {read(t); read(args...); }

struct node
{
	int l,r,v;
	bool operator<(const node &tmp) const
	{
		return l < tmp.l;
	}
};
int n,k,l[2005],r[2005],a[2005];
map<int,int> dp[N];

signed main()
{
	read(n,k);
	for (int i = 1; i <= n; i++)
		read(l[i],r[i],a[i]);
	for (int i = 1; i <= n; i++)
	{
		if (a[i] > (r[i] - l[i] + 1) * k)
		{
			puts("-1");
			return 0;
		}
	}
	dp[0][k] = 0;
	for (int i = 1; i <= n; i++)
	{
		dp[i][k] = 0x7f7f7f7f7f7f7f;
		for (map<int,int>::iterator it = dp[i - 1].begin(); it != dp[i - 1].end(); it++)
		{
			int x = it -> first,y = it -> second;
			if (y == 0x7f7f7f7f7f7f7f) continue;
			if (x + (r[i] - l[i]) * k < a[i]) continue;
			if (x >= a[i])
			{
				if (dp[i].find(x - a[i]) == dp[i].end()) dp[i][x - a[i]] = 0x7f7f7f7f7f7f7f;
				dp[i][x - a[i]] = min(dp[i][x - a[i]],y + a[i]);
				if (l[i + 1] != l[i]) dp[i][k] = min(dp[i][k],x + y);
				continue;
			}
			int tmp = (x - a[i] % k + k) % k;
			if (dp[i].find(tmp) == dp[i].end()) dp[i][tmp] = 0x7f7f7f7f7f7f7f;
			if ((x + max(0LL,r[i] - l[i] - 1) * k >= a[i] || l[i + 1] != r[i]) && l[i + 1] != l[i])
			{
				dp[i][k] = min(dp[i][k],y + tmp + a[i]);
			}
			dp[i][tmp] = min(dp[i][tmp],y + a[i]);
		}
	}
	int ans = 0x7f7f7f7f7f7f7f;
	for (map<int,int>::iterator it = dp[n].begin(); it != dp[n].end(); it++)
		ans = min(ans,it -> second);
	if (ans == 0x7f7f7f7f7f7f7f) puts("-1");
	else printf("%lld\n",ans);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2010;

int n, k, a[N], l[N], r[N], dp[N];

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	cin >> n >> k;
	for (int i = 1; i <= n; ++i) {
		cin >> l[i] >> r[i] >> a[i];
	}
	for (int i = n; i >= 1; --i) {
		int demand = a[i];
		if (r[i] == l[i + 1] && i != n) demand += dp[i + 1];
		if (demand > 1ll * (r[i] - l[i] + 1) * k) {
			cout << "-1\n";
			return 0;
		}
		dp[i] = max(0ll, demand - 1ll * (r[i] - l[i]) * k);
	}
	int cur = k;
	long long ans = 0;
	for (int i = 1; i <= n; ++i) {
		if (cur < dp[i]) {
			ans += cur;
			cur = k;
		}
		ans += a[i];
		cur = ((cur - a[i]) % k + k) % k;
	}
	cout << ans << '\n';

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2010;

int n, k, a[N], l[N], r[N], dp[N];

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	cin >> n >> k;
	for (int i = 1; i <= n; ++i) {
		cin >> l[i] >> r[i] >> a[i];
	}
	for (int i = n; i >= 1; --i) {
		int demand = a[i];
		if (r[i] == l[i + 1] && i != n) demand += dp[i + 1];
		if (demand > 1ll * (r[i] - l[i] + 1) * k) {
			cout << "-1\n";
			return 0;
		}
		dp[i] = demand - 1ll * (r[i] - l[i]) * k;
	}
	int cur = k;
	long long ans = 0;
	for (int i = 1; i <= n; ++i) {
		if (cur < dp[i]) {
			ans += cur;
			cur = k;
		}
		ans += a[i];
		cur = ((cur - a[i]) % k + k) % k;
	}
	cout << ans << '\n';

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2010;

int n, k, a[N], l[N], r[N], dp[N];

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	cin >> n >> k;
	for (int i = 1; i <= n; ++i) {
		cin >> l[i] >> r[i] >> a[i];
	}
	for (int i = n; i >= 1; --i) {
		int demand = a[i];
		if (r[i] == l[i + 1] && i != n) demand += dp[i + 1];
		if (demand > 1ll * (r[i] - l[i] + 1) * k) {
			cout << "-1\n";
			return 0;
		}
		dp[i] = max(0, demand - 1ll * (r[i] - l[i]) * k);
	}
	int cur = k;
	long long ans = 0;
	for (int i = 1; i <= n; ++i) {
		if (cur < dp[i]) {
			ans += cur;
			cur = k;
		}
		ans += a[i];
		cur = ((cur - a[i]) % k + k) % k;
	}
	cout << ans << '\n';

	return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,k,rem,sum,ans,flg;
int l[2010],r[2010],a[2010],f[2010],s[2010];
int read(){
    int r=0,f=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c<='9'&&c>='0') r=(r<<1)+(r<<3)+c-'0',c=getchar();
    return r*f;
}
void put(int x){
    if(x<0){putchar('-');x=~x+1;}
    if(x>9) put(x/10);
    putchar(x%10+'0');
}
signed main(){
    n=read(),k=read();
    for(int i=1;i<=n;i++) l[i]=read(),r[i]=read(),a[i]=read(),s[i]=s[i-1]+a[i];
    for(int i=n;i>=1;i--)
        if(r[i]==l[i+1]) f[i]=max(0ll,a[i]+f[i+1]-k*(r[i]-l[i]));
        else f[i]=max(0ll,a[i]-k*(r[i]-l[i]));
    for(int i=1;i<=n;i++)
        for(int j=i;j<=n;j++)
            if(s[j]-s[i-1]>(r[j]-l[i]+1)*k) flg=1;
    for(int i=1;i<=n;i++){
        if(rem<f[i]) ans+=rem,rem=k;
        rem-=a[i],rem%=k;
        if(rem<0) rem+=k;
    }
    if(flg){puts("-1");return 0;}
    put(s[n]+ans);puts("");
    return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,k,rem,sum,ans;
int l[2010],r[2010],a[2010],f[2010];
int read(){
    int r=0,f=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c<='9'&&c>='0') r=(r<<1)+(r<<3)+c-'0',c=getchar();
    return r*f;
}
void put(int x){
    if(x<0){putchar('-');x=~x+1;}
    if(x>9) put(x/10);
    putchar(x%10+'0');
}
signed main(){
    n=read(),k=read();
    for(int i=1;i<=n;i++) l[i]=read(),r[i]=read(),a[i]=read(),sum+=a[i];
    for(int i=n;i>=1;i--){
        if(r[i]==l[i+1]) f[i]=max(0ll,a[i]+f[i+1]-k*(r[i]-l[i]));
        else f[i]=max(0ll,a[i]-k*(r[i]-l[i]));
        int tmp=f[i],tap=a[i];
        if(l[i]==l[i+1]) tmp+=f[i+1],tap+=a[i+1];
        if(tmp>tap){puts("-1");return 0;}
    }
    for(int i=1;i<=n;i++){
        if(rem<f[i]) ans+=rem,rem=k;
        rem-=a[i],rem%=k;
        if(rem<0) rem+=k;
    }
    put(sum+ans);puts("");
    return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,k,rem,sum,ans;
int l[2010],r[2010],a[2010],f[2010];
int read(){
    int r=0,f=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c<='9'&&c>='0') r=(r<<1)+(r<<3)+c-'0',c=getchar();
    return r*f;
}
void put(int x){
    if(x<0){putchar('-');x=~x+1;}
    if(x>9) put(x/10);
    putchar(x%10+'0');
}
signed main(){
    n=read(),k=read();
    for(int i=1;i<=n;i++) l[i]=read(),r[i]=read(),a[i]=read(),sum+=a[i];
    if(sum>(r[n]-l[1]+1)*k){puts("-1");return 0;}
    for(int i=n;i>=1;i--)
        if(r[i]==l[i+1]) f[i]=max(0ll,a[i]+f[i+1]-k*(r[i]-l[i]));
        else f[i]=max(0ll,a[i]-k*(r[i]-l[i]));
    for(int i=1;i<=n;i++){
        if(rem<f[i]) ans+=rem,rem=k;
        rem-=a[i],rem%=k;
        if(rem<0) rem+=k;
    }
    put(sum+ans);puts("");
    return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,k,rem,sum,ans;
int l[2010],r[2010],a[2010],f[2010];
int read(){
    int r=0,f=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c<='9'&&c>='0') r=(r<<1)+(r<<3)+c-'0',c=getchar();
    return r*f;
}
void put(int x){
    if(x<0){putchar('-');x=~x+1;}
    if(x>9) put(x/10);
    putchar(x%10+'0');
}
signed main(){
    n=read(),k=read();
    for(int i=1;i<=n;i++) l[i]=read(),r[i]=read(),a[i]=read(),sum+=a[i];
    for(int i=n;i>=1;i--){
        if(r[i]==l[i+1]) f[i]=max(0ll,a[i]+f[i+1]-k*(r[i]-l[i]));
        else f[i]=max(0ll,a[i]-k*(r[i]-l[i]));
        if(f[i]>a[i]){puts("-1");return 0;}
    }
    for(int i=1;i<=n;i++){
        if(rem<f[i]) ans+=rem,rem=k;
        rem-=a[i],rem%=k;
        if(rem<0) rem+=k;
    }
    put(sum+ans);puts("");
    return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,k,p,ans,flg;
int l[2010],r[2010],a[2010];
int read(){
    int r=0,f=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c<='9'&&c>='0') r=(r<<1)+(r<<3)+c-'0',c=getchar();
    return r*f;
}
void put(int x){
    if(x<0){putchar('-');x=~x+1;}
    if(x>9) put(x/10);
    putchar(x%10+'0');
}
signed main(){
    n=read(),k=read();
    for(int i=1;i<=n;i++){
        l[i]=read(),r[i]=read(),a[i]=read();
        if(flg) continue;
        int nd=(a[i]+k-1)/k;
        if(l[i]==r[i-1]&&p){
            if(nd>r[i]-l[i]) flg=1;
            else if(nd>r[i]-l[i]-1) p=1;
            else p=0;
        }
        else{
            if(nd>r[i]-l[i]+1) flg=1;
            else if(nd>r[i]-l[i]) p=1;
            else p=0;
        }
        if(i==n) ans+=a[i];
        else ans+=nd*k;
    }
    if(flg){puts("-1");return 0;}
    put(ans);puts("");
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
int n,k;
struct node
{
    ll l,r,w;
}a[2005];
ll dp[2005];
int main()
{
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i].l>>a[i].r>>a[i].w;
    }
    for(int i=n;i>=1;i--)
    {
        ll need=a[i].w;
        if(a[i].r==a[i+1].l) need+=dp[i+1];
        if((a[i].r-a[i].l+1)*k<need)
        {
            cout<<-1<<endl;
            return 0;
        }
        dp[i]=max((ll)0,need-(a[i].r-a[i].l)*k);
    }
    ll ans=0,res=k;
    for(int i=1;i<=n;i++)
    {
        if(res<dp[i])
        {
            ans+=res;
            res=k;
        }
        ans+=a[i].w;
        res=((res-a[i].w)%k+k)%k;
    }
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define ri register
#define mk make_pair
#define E (m+1)
using namespace std; const int N=4010;
inline int read()
{
	int s=0, w=1; ri char ch=getchar();
	while(ch<'0'||ch>'9') { if(ch=='-') w=-1; ch=getchar(); }
	while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48), ch=getchar();
	return s*w;
}
int n,K,F[N],Ans;
int l[N],r[N],a[N];
signed main()
{
	n=read(), K=read();
	for(ri int i=1;i<=n;i++) l[i]=read(), r[i]=read(), a[i]=read();
	for(ri int i=n;i;i--)
	{
		int gt=((i<n && r[i]==l[i+1])?(F[i+1]):(0));
		gt+=a[i];
		if((r[i]-l[i]+1)*K<gt) return puts("-1")&0;
		F[i]=max(0ll,gt-(r[i]-l[i])*K);
	}
	int now=K;
	for(ri int i=1;i<=n;i++)
	{
		if(F[i]>now)
		{
			Ans+=now;
			now=K;
		}
		Ans+=a[i];
		now=(now-a[i])%K;
		now=(now%K+K)%K;
	}
	printf("%lld\n",Ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 4005;
long long a[N], dp[N], l[N], r[N];
int main() {
	int n;
	long long k;
	scanf("%d%lld", &n, &k);
	for (int i = 1; i <= n; i ++) {
        scanf("%lld%lld%lld", l + i, r + i, a + i);
	}
	long long ans = 1e18, tot;
	for (int i = 1; i <= n; i ++) dp[i] = 1e18;
	dp[1] = 0;
	for (int i = 1; i <= n; i ++) {
		tot = dp[i];
		long long res = k;
	    for (int j = i; j <= n; j ++){
	 	    long long cnt = (max(0ll, a[j] - res) + k - 1) / k;
		    if (cnt > r[j] - l[j]) break;
		    long long nct = (res + cnt * k) - a[j];
			tot += a[j];
		    if (j < n) {
		    	if (l[j] + cnt < l[j + 1]) dp[j + 1] = min(dp[j + 1], tot + nct);
		    }
			else {
				ans = min(ans, tot);
			}
			res = nct;

	   }
	}
	if (ans == 1e18) puts("-1");
	else printf("%lld\n", ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e3 + 10;
typedef long long ll;
const ll inf = 1e15;
#define pii pair<int,int>
#define fir first
#define sec second
int n, k;
ll dp[maxn], sum = 0;
struct node {
	int l, r, v;
	node() {}
	node(int li,int ri,int vi) {
		l = li;
		r = ri;
		v = vi;
	}
} a[maxn];
pii calc(int num,int p) {				//打完每一波的剩余子弹，和剩余时间,剩余时间小于 0 说明无解
	int l = a[p].l, r = a[p].r, v = a[p].v;
	if (v <= num) return pii(num - v,r - l);
	else {
		int left = r - l - 1;
		v -= num;
		return pii((k - v % k) % k,left - v / k - (v % k == 0));
	}
}
int main() {
	scanf("%d%d",&n,&k);
	for (int i = 1; i <= n; i++)
		scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].v), sum += a[i].v;
	for (int i = 1; i <= n; i++)
		dp[i] = inf;
	dp[n] = 0;
	for (int i = n; i >= 1; i--) {
		int num = k;
		for (int j = i + 1; j <= n; j++) {
			pii tmp = calc(num,j - 1);
			if (tmp.sec < 0) break;
			if (tmp.sec > 0)
				dp[i] = min(dp[i],dp[j] + tmp.fir);
			num = tmp.fir;
		}
	}
	//printf("%d\n",calc(k,1).fir);
	printf("%lld\n",dp[1] == inf ? -1 : dp[1] + sum);

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define fir first
#define sec second
typedef long long ll;
int n, k;
int main() {
	ll ans = 0;
	scanf("%d%d",&n,&k);
	int st = 0;
	bool f = true;
	for (int i = 1; i <= n; i++) {
		int l, r, a, b; scanf("%d%d%d",&l,&r,&a);
		b = a + (k - a % k) % k;
		st = max(st,l);
		if (st + b / k - 1 > r) {
			f = false;
		}
		st += b / k;
		if (i < n)
			ans += b;
		else
			ans += a;
	}
	if (!f) puts("-1");
	else printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e3 + 10;
typedef long long ll;
const ll inf = 1e15;
#define pii pair<int,int>
#define fir first
#define sec second
int n, k;
ll dp[maxn], sum = 0;
struct node {
	int l, r, v;
	node() {}
	node(int li,int ri,int vi) {
		l = li;
		r = ri;
		v = vi;
	}
} a[maxn];
pii calc(int num,int p) {				//打完每一波的剩余子弹，和剩余时间,剩余时间小于 0 说明无解
	int l = a[p].l, r = a[p].r, v = a[p].v;
	if (v <= num) return pii(num - v,r - l);
	else {
		int left = r - l - 1;
		v -= num;
		return pii((k - v % k) % k,left - (v / k - (v % k == 0)));
	}
}
int main() {
	scanf("%d%d",&n,&k);
	for (int i = 1; i <= n; i++)
		scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].v), sum += a[i].v;
	for (int i = 1; i <= n; i++)
		dp[i] = inf;
	dp[n + 1] = 0;
	for (int i = n; i >= 1; i--) {
		int num = k;
		for (int j = i; j <= n; j++) {
			pii tmp = calc(num,j);
			if (tmp.sec < 0) break;
			if (j == n) {
				dp[i] = 0;
				break;
			}
			if (tmp.sec + a[j + 1].l - a[j].r > 0) dp[i] = min(dp[i],dp[j + 1] + tmp.fir);
			num = tmp.fir;
		}
		//printf("****%d %lld\n",i,dp[i]);
	}
	//printf("***%d %d\n",calc(45,6).sec,calc(45,6).fir);
	printf("%lld\n",dp[1] == inf ? -1 : dp[1] + sum);

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e3 + 10;
typedef long long ll;
const ll inf = 1e15;
#define pii pair<int,int>
#define fir first
#define sec second
int n, k;
ll dp[maxn], sum = 0;
struct node {
	int l, r, v;
	node() {}
	node(int li,int ri,int vi) {
		l = li;
		r = ri;
		v = vi;
	}
} a[maxn];
pii calc(int num,int p) {				//打完每一波的剩余子弹，和剩余时间,剩余时间小于 0 说明无解
	int l = a[p].l, r = a[p].r, v = a[p].v;
	if (v <= num) return pii(num - v,r - l);
	else {
		int left = r - l - 1;
		v -= num;
		return pii((k - v % k) % k,left - (v / k - (v % k == 0)));
	}
}
int main() {
	scanf("%d%d",&n,&k);
	for (int i = 1; i <= n; i++)
		scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].v), sum += a[i].v;
	for (int i = 1; i <= n; i++)
		dp[i] = inf;
	dp[n + 1] = 0;
	for (int i = n; i >= 1; i--) {
		int num = k;
		for (int j = i; j <= n; j++) {
			pii tmp = calc(num,j);
			if (tmp.sec < 0) break;
			if (tmp.sec > 0) {
				dp[i] = min(dp[i],dp[j + 1] + tmp.fir);
				break;
			}
			num = tmp.fir;
			if (j == n) dp[i] = 0;
		}
	}
	//printf("%d\n",calc(k,1).sec);
	printf("%lld\n",dp[1] == inf ? -1 : dp[1] + sum);

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e3 + 10;
typedef long long ll;
const ll inf = 1e15;
#define pii pair<int,int>
#define fir first
#define sec second
int n, k;
ll dp[maxn], sum = 0;
struct node {
	int l, r, v;
	node() {}
	node(int li,int ri,int vi) {
		l = li;
		r = ri;
		v = vi;
	}
} a[maxn];
pii calc(int num,int p) {				//打完每一波的剩余子弹，和剩余时间,剩余时间小于 0 说明无解
	int l = a[p].l, r = a[p].r, v = a[p].v;
	if (v <= num) return pii(num - v,r - l);
	else {
		int left = r - l - 1;
		v -= num;
		return pii((k - v % k) % k,left - (v / k - (v % k == 0)));
	}
}
int main() {
	scanf("%d%d",&n,&k);
	for (int i = 1; i <= n; i++)
		scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].v), sum += a[i].v;
	for (int i = 1; i <= n; i++)
		dp[i] = inf;
	dp[n + 1] = 0;
	for (int i = n; i >= 1; i--) {
		int num = k;
		for (int j = i; j <= n; j++) {
			pii tmp = calc(num,j);
			if (tmp.sec < 0) break;
			if (tmp.sec > 0) dp[i] = min(dp[i],dp[j + 1] + tmp.fir);
			num = tmp.fir;
			if (j == n) dp[i] = 0;
		}
	}
	//printf("%d\n",calc(k,1).sec);
	printf("%lld\n",dp[1] == inf ? -1 : dp[1] + sum);

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#define int long long
using namespace std;
int n,k,l[2005],r[2005],a[2005],sum[2005],f[2005],ans=1e18;
signed main(){
	cin>>n>>k;
	for (int i=1;i<=n;i++)cin>>l[i]>>r[i]>>a[i],sum[i]=sum[i-1]+a[i];
	memset(f,0x3f,sizeof(f));
	f[1]=0;
	for (int i=1;i<=n;i++){
		if (f[i]>=1e18)continue;
		int rest=k;
		for (int j=i;j<=n;j++){
			int cnt;
			if (rest>=a[j])rest-=a[j],cnt=0;
			else{
				cnt=(a[j]-rest-1)/k+1;
				if (cnt>r[j]-l[j])break;
			}
			if (j==n)ans=min(ans,f[i]+sum[j]-sum[i-1]);
			else{
				if (cnt<r[j]-l[j]||r[j]!=l[j+1])f[j+1]=min(f[j+1],((sum[j]-sum[i-1]-1)/k+1)*k);
				rest=rest+cnt*k-a[i];
			}
		}
	}
	if (ans<1e18)cout<<ans<<endl;
	else cout<<-1<<endl;
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#define int long long
using namespace std;
int n,k,l[2005],r[2005],a[2005],sum[2005],f[2005],ans=1e18;
signed main(){
	cin>>n>>k;
	for (int i=1;i<=n;i++)cin>>l[i]>>r[i]>>a[i],sum[i]=sum[i-1]+a[i];
	memset(f,0x3f,sizeof(f));
	f[1]=0;
	for (int i=1;i<=n;i++){
		if (f[i]>=1e18)continue;
		int rest=k;
		for (int j=i;j<=n;j++){
			int cnt;
			if (rest>=a[j])cnt=0;
			else{
				cnt=(a[j]-rest-1)/k+1;
				if (cnt>r[j]-l[j])break;
			}
			if (j==n)ans=min(ans,f[i]+sum[j]-sum[i-1]);
			else{
				if (cnt<r[j]-l[j]||r[j]!=l[j+1])f[j+1]=min(f[j+1],f[i]+((sum[j]-sum[i-1]-1)/k+1)*k);
				rest=rest+cnt*k-a[j];
			}
		}
	}
	if (ans<1e18)cout<<ans<<endl;
	else cout<<-1<<endl;
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#define int long long
using namespace std;
int n,k,l[2005],r[2005],a[2005],sum[2005],f[2005],ans=1e18;
signed main(){
	cin>>n>>k;
	for (int i=1;i<=n;i++)cin>>l[i]>>r[i]>>a[i],sum[i]=sum[i-1]+a[i];
	memset(f,0x3f,sizeof(f));
	f[1]=0;
	for (int i=1;i<=n;i++){
		int rest=k;
		for (int j=i;j<=n;j++){
			int cnt;
			if (rest>=a[j])rest-=a[j],cnt=0;
			else{
				cnt=(a[j]-rest-1)/k+1;
				if (cnt>r[j]-l[j])break;
			}
			if (j==n)ans=min(ans,f[i]+sum[j]-sum[i-1]);
			else{
				if (cnt<r[j]-l[j]||r[j]!=l[j+1])f[j+1]=min(f[j+1],((sum[j]-sum[i-1]-1)/k+1)*k);
				rest=rest+cnt*k-a[i];
			}
		}
	}
	if (ans<1e18)cout<<ans<<endl;
	else cout<<-1<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define int long long
#define N 2005
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define lowbit(i) ((i)&(-i))
#define VI vector<int>
using namespace std;
int n,k,l[N],r[N],a[N],f[N];
signed main(){
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	scanf("%lld%lld",&n,&k);
	rep(i,1,n) scanf("%lld%lld%lld",&l[i],&r[i],&a[i]);
	memset(f,0x3f,sizeof f);
	f[1] = 0;
	rep(i,1,n){
		int now = k,ans = f[i];
		rep(j,i,n){
			int ti = ((max(0ll,a[j]-now)+k-1)/k);
			if(ti > r[j]-l[j]) break;
			int nxt = (now + k*ti) - a[j];
			ans += a[j];
			if(j+1 <= n){
				if(l[j]+ti < l[j+1]) f[j+1] = min(f[j+1],ans+nxt);
			}else{
				f[j+1] = min(f[j+1],ans);
			}
			now = nxt;
		}
	}
	if(f[n+1] >= inf) puts("-1");
	else printf("%lld\n",f[n+1]);
	return 0;
}


#include<bits/stdc++.h>
#define int long long
#define N 2005
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define inf 0x3f3f3f3f3f3f3f3f
#define pb push_back
#define mp make_pair
#define lowbit(i) ((i)&(-i))
#define VI vector<int>
using namespace std;
int n,k,l[N],r[N],a[N],f[N];
signed main(){
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	scanf("%lld%lld",&n,&k);
	rep(i,1,n) scanf("%lld%lld%lld",&l[i],&r[i],&a[i]);
	memset(f,0x3f,sizeof f);
	f[1] = 0;
	rep(i,1,n){
		int now = k,ans = f[i];
		rep(j,i,n){
			int ti = ((max(0ll,a[j]-now)+k-1)/k);
			if(ti > r[j]-l[j]) break;
			int nxt = (now + k*ti) - a[j];
			ans += a[j];
			if(j+1 <= n){
				if(l[j]+ti < l[j+1]) f[j+1] = min(f[j+1],ans+nxt);
			}else{
				f[j+1] = min(f[j+1],ans);
			}
			now = nxt;
		}
	}
	if(f[n+1] >= inf) puts("-1");
	else printf("%lld\n",f[n+1]);
	return 0;
}


/*++++++++++++++++++++++++++++++
 File Name: code.cpp
 Author: Moonlights
 Created Time: 2020年11月04日 星期三 00时01分10秒
++++++++++++++++++++++++++++++*/

/*CYJian yyds!*/
/*ldx gg txdy!*/

// {{{ Template
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>

typedef std::vector <int> poly;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

#define pb push_back
#define pp pop_back
#define fi first
#define se second
#define rez resize
#define mkp std::make_pair
#define pii std::pair <int,int>
#define lep(i,l,r) for(int i=l;i<=r;++i)
#define rep(i,r,l) for(int i=r;i>=l;--i)
#define CLEAR(x) memset((x),0,sizeof((x)))
#define COPY(x,y) memcpy((x),(y),sizeof(x))

const int inf=1e9+7;
const int mod=998244353;

namespace __________ {
	namespace _in {
		template <class T> inline T read() {
			char ch;bool flag=0;T x=0;
			while(ch=getchar(),!isdigit(ch)) if(ch=='-') flag=1;
			while(isdigit(ch)) x=x*10+ch-48,ch=getchar();
			return flag?-x:x;
		}
		struct {
			inline operator ll() {return read<ll>();}
			inline operator int() {return read<int>();}
			inline operator bool() {return read<bool>();}
			template <class T> inline void operator() (T &x) {x=*this;}
			template <class T,class ...A> inline void operator() (T &x,A &...a)
			{x=*this,this->operator()(a...);}
		} IN;
	}
	namespace _std {
		template <class T> inline T abs(T x) {if(x<0) x=-x;return x;}
		template <class T> inline T max(T x,T y) {if(x<y) x=y;return x;}
		template <class T> inline T min(T x,T y) {if(x>y) x=y;return x;}
		template <class T> inline void swap(T &x,T &y) {x^=y^=x^=y;}
		template <class T> inline void chkmax(T&x,T y) {if(x<y) x=y;}
		template <class T> inline void chkmin(T&x,T y) {if(x>y) x=y;}
	}
	namespace _mod {
		inline int mul(int x,int y) {return 1ll*x*y%mod;}
		inline int dec(int x,int y) {x-=y;if(x<0) x+=mod;return x;}
		inline int add(int x,int y) {x+=y;if(x>=mod) x-=mod;return x;}
		inline void pls(int&x,int y) {x+=y;if(x>=mod) x-=mod;}
		inline void sub(int&x,int y) {x-=y;if(x<0) x+=mod;}
		inline int modpow(int x,int y,int res=1) {
			if(y==-1) y=mod-2;
			for(;y;y>>=1,x=mul(x,x)) if(y&1) res=mul(res,x);
			return res;
		}
	}

	using namespace _in;
	using namespace _std;
	using namespace _mod;
} using namespace __________;
// }}}

const int N=2e3+5;
int n,k,L[N],R[N],a[N];
ll dp[N],sum;

inline void calc(int &tot,int p,int &tim) {
	int _,a=::a[p],L=::L[p];
	if(a<=tot) tot-=a,tim=L+1;
	else _=a-tot,tot=k-(_%k),tim=L+(_/k-(_%k==0))+1;
}
inline void solve() {
	IN(n,k);
	lep(i,1,n) IN(L[i],R[i],a[i]),sum+=a[i];

	dp[n+1]=0;
	rep(i,n,1) {
		dp[i]=1e18;

		int tot=k,tim;
		lep(j,i,n) {
			calc(tot,j,tim); if(tim>R[j]) break;
			if(j==n) dp[i]=0;
			if(L[j+1]>tim) chkmin(dp[i],dp[j+1]+tot);
		}
	}
	printf("%lld\n",dp[1]>1e14?-1:dp[1]+sum);
}

int main() {
	int T=1;
	// IN(T);
	while(T--) solve();
	return 0;
}


/*++++++++++++++++++++++++++++++
 File Name: code.cpp
 Author: Moonlights
 Created Time: 2020年11月04日 星期三 00时01分10秒
++++++++++++++++++++++++++++++*/

/*CYJian yyds!*/
/*ldx gg txdy!*/

// {{{ Template
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>

typedef std::vector <int> poly;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

#define pb push_back
#define pp pop_back
#define fi first
#define se second
#define rez resize
#define mkp std::make_pair
#define pii std::pair <int,int>
#define lep(i,l,r) for(int i=l;i<=r;++i)
#define rep(i,r,l) for(int i=r;i>=l;--i)
#define CLEAR(x) memset((x),0,sizeof((x)))
#define COPY(x,y) memcpy((x),(y),sizeof(x))

const int inf=1e9+7;
const int mod=998244353;

namespace __________ {
	namespace _in {
		template <class T> inline T read() {
			char ch;bool flag=0;T x=0;
			while(ch=getchar(),!isdigit(ch)) if(ch=='-') flag=1;
			while(isdigit(ch)) x=x*10+ch-48,ch=getchar();
			return flag?-x:x;
		}
		struct {
			inline operator ll() {return read<ll>();}
			inline operator int() {return read<int>();}
			inline operator bool() {return read<bool>();}
			template <class T> inline void operator() (T &x) {x=*this;}
			template <class T,class ...A> inline void operator() (T &x,A &...a)
			{x=*this,this->operator()(a...);}
		} IN;
	}
	namespace _std {
		template <class T> inline T abs(T x) {if(x<0) x=-x;return x;}
		template <class T> inline T max(T x,T y) {if(x<y) x=y;return x;}
		template <class T> inline T min(T x,T y) {if(x>y) x=y;return x;}
		template <class T> inline void swap(T &x,T &y) {x^=y^=x^=y;}
		template <class T> inline void chkmax(T&x,T y) {if(x<y) x=y;}
		template <class T> inline void chkmin(T&x,T y) {if(x>y) x=y;}
	}
	namespace _mod {
		inline int mul(int x,int y) {return 1ll*x*y%mod;}
		inline int dec(int x,int y) {x-=y;if(x<0) x+=mod;return x;}
		inline int add(int x,int y) {x+=y;if(x>=mod) x-=mod;return x;}
		inline void pls(int&x,int y) {x+=y;if(x>=mod) x-=mod;}
		inline void sub(int&x,int y) {x-=y;if(x<0) x+=mod;}
		inline int modpow(int x,int y,int res=1) {
			if(y==-1) y=mod-2;
			for(;y;y>>=1,x=mul(x,x)) if(y&1) res=mul(res,x);
			return res;
		}
	}

	using namespace _in;
	using namespace _std;
	using namespace _mod;
} using namespace __________;
// }}}

const int N=2e3+5;
int n,k,L[N],R[N],a[N];
ll dp[N],sum;

inline void calc(int &tot,int p,int &tim) {
	int _,a=::a[p],L=::L[p];
	if(a<=tot) tot-=a,tim=L;
	else _=a-tot,tot=k-(_%k),tim=L+(_/k-(_%k==0))-1;
}
inline void solve() {
	IN(n,k);
	lep(i,1,n) IN(L[i],R[i],a[i]),sum+=a[i];

	dp[n+1]=0;
	rep(i,n,1) {
		dp[i]=1e18;

		int tot=k,tim;
		lep(j,i,n) {
			calc(tot,j,tim); if(tim>R[j]) break;
			if(j==n) dp[i]=0;
			if(L[j+1]>tim+1) chkmin(dp[i],dp[j+1]+tot);
		}
	}
	printf("%lld\n",dp[1]>1e14?-1:dp[1]+sum);
}

int main() {
	int T=1;
	// IN(T);
	while(T--) solve();
	return 0;
}


/*++++++++++++++++++++++++++++++
 File Name: code.cpp
 Author: Moonlights
 Created Time: 2020年11月04日 星期三 00时01分10秒
++++++++++++++++++++++++++++++*/

/*CYJian yyds!*/
/*ldx gg txdy!*/

// {{{ Template
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>

typedef std::vector <int> poly;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

#define pb push_back
#define pp pop_back
#define fi first
#define se second
#define rez resize
#define mkp std::make_pair
#define pii std::pair <int,int>
#define lep(i,l,r) for(int i=l;i<=r;++i)
#define rep(i,r,l) for(int i=r;i>=l;--i)
#define CLEAR(x) memset((x),0,sizeof((x)))
#define COPY(x,y) memcpy((x),(y),sizeof(x))

const int inf=1e9+7;
const int mod=998244353;

namespace __________ {
	namespace _in {
		template <class T> inline T read() {
			char ch;bool flag=0;T x=0;
			while(ch=getchar(),!isdigit(ch)) if(ch=='-') flag=1;
			while(isdigit(ch)) x=x*10+ch-48,ch=getchar();
			return flag?-x:x;
		}
		struct {
			inline operator ll() {return read<ll>();}
			inline operator int() {return read<int>();}
			inline operator bool() {return read<bool>();}
			template <class T> inline void operator() (T &x) {x=*this;}
			template <class T,class ...A> inline void operator() (T &x,A &...a)
			{x=*this,this->operator()(a...);}
		} IN;
	}
	namespace _std {
		template <class T> inline T abs(T x) {if(x<0) x=-x;return x;}
		template <class T> inline T max(T x,T y) {if(x<y) x=y;return x;}
		template <class T> inline T min(T x,T y) {if(x>y) x=y;return x;}
		template <class T> inline void swap(T &x,T &y) {x^=y^=x^=y;}
		template <class T> inline void chkmax(T&x,T y) {if(x<y) x=y;}
		template <class T> inline void chkmin(T&x,T y) {if(x>y) x=y;}
	}
	namespace _mod {
		inline int mul(int x,int y) {return 1ll*x*y%mod;}
		inline int dec(int x,int y) {x-=y;if(x<0) x+=mod;return x;}
		inline int add(int x,int y) {x+=y;if(x>=mod) x-=mod;return x;}
		inline void pls(int&x,int y) {x+=y;if(x>=mod) x-=mod;}
		inline void sub(int&x,int y) {x-=y;if(x<0) x+=mod;}
		inline int modpow(int x,int y,int res=1) {
			if(y==-1) y=mod-2;
			for(;y;y>>=1,x=mul(x,x)) if(y&1) res=mul(res,x);
			return res;
		}
	}

	using namespace _in;
	using namespace _std;
	using namespace _mod;
} using namespace __________;
// }}}

const int N=2e3+5;
int n,k,L[N],R[N],a[N];
ll dp[N],sum;

inline void calc(int &tot,int p,int &tim) {
	int _,a=::a[p],L=::L[p];
	if(a<=tot) tot-=a,tim=L;
	else _=a-tot,tot=(k-(_%k))%k,tim=L+(_/k-(_%k==0))+1;
}
inline void solve() {
	IN(n,k);
	lep(i,1,n) IN(L[i],R[i],a[i]),sum+=a[i];

	dp[n+1]=0;
	rep(i,n,1) {
		dp[i]=1e18;

		int tot=k,tim;
		lep(j,i,n) {
			calc(tot,j,tim); if(tim>R[j]) break;
			if(j==n) dp[i]=0;
			if(L[j+1]>tim) chkmin(dp[i],dp[j+1]+tot);
		}
	}
	printf("%lld\n",dp[1]>1e14?-1:dp[1]+sum);
}

int main() {
	int T=1;
	// IN(T);
	while(T--) solve();
	return 0;
}


/*++++++++++++++++++++++++++++++
 File Name: code.cpp
 Author: Moonlights
 Created Time: 2020年11月04日 星期三 00时01分10秒
++++++++++++++++++++++++++++++*/

/*CYJian yyds!*/
/*ldx gg txdy!*/

// {{{ Template
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>

typedef std::vector <int> poly;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

#define pb push_back
#define pp pop_back
#define fi first
#define se second
#define rez resize
#define mkp std::make_pair
#define pii std::pair <int,int>
#define lep(i,l,r) for(int i=l;i<=r;++i)
#define rep(i,r,l) for(int i=r;i>=l;--i)
#define CLEAR(x) memset((x),0,sizeof((x)))
#define COPY(x,y) memcpy((x),(y),sizeof(x))

const int inf=1e9+7;
const int mod=998244353;

namespace __________ {
	namespace _in {
		template <class T> inline T read() {
			char ch;bool flag=0;T x=0;
			while(ch=getchar(),!isdigit(ch)) if(ch=='-') flag=1;
			while(isdigit(ch)) x=x*10+ch-48,ch=getchar();
			return flag?-x:x;
		}
		struct {
			inline operator ll() {return read<ll>();}
			inline operator int() {return read<int>();}
			inline operator bool() {return read<bool>();}
			template <class T> inline void operator() (T &x) {x=*this;}
			template <class T,class ...A> inline void operator() (T &x,A &...a)
			{x=*this,this->operator()(a...);}
		} IN;
	}
	namespace _std {
		template <class T> inline T abs(T x) {if(x<0) x=-x;return x;}
		template <class T> inline T max(T x,T y) {if(x<y) x=y;return x;}
		template <class T> inline T min(T x,T y) {if(x>y) x=y;return x;}
		template <class T> inline void swap(T &x,T &y) {x^=y^=x^=y;}
		template <class T> inline void chkmax(T&x,T y) {if(x<y) x=y;}
		template <class T> inline void chkmin(T&x,T y) {if(x>y) x=y;}
	}
	namespace _mod {
		inline int mul(int x,int y) {return 1ll*x*y%mod;}
		inline int dec(int x,int y) {x-=y;if(x<0) x+=mod;return x;}
		inline int add(int x,int y) {x+=y;if(x>=mod) x-=mod;return x;}
		inline void pls(int&x,int y) {x+=y;if(x>=mod) x-=mod;}
		inline void sub(int&x,int y) {x-=y;if(x<0) x+=mod;}
		inline int modpow(int x,int y,int res=1) {
			if(y==-1) y=mod-2;
			for(;y;y>>=1,x=mul(x,x)) if(y&1) res=mul(res,x);
			return res;
		}
	}

	using namespace _in;
	using namespace _std;
	using namespace _mod;
} using namespace __________;
// }}}

const int N=2e3+5;
int n,k,L[N],R[N],a[N];
ll dp[N],sum;

inline void calc(int &tot,int p,int &tim) {
	int _,a=::a[p],L=::L[p];
	if(a<=tot) tot-=a,tim=L+1;
	else _=a-tot,tot=(k-(_%k))%k,tim=L+(_/k-(_%k==0))+1;
}
inline void solve() {
	IN(n,k);
	lep(i,1,n) IN(L[i],R[i],a[i]),sum+=a[i];

	dp[n+1]=0;
	rep(i,n,1) {
		dp[i]=1e18;

		int tot=k,tim;
		lep(j,i,n) {
			calc(tot,j,tim); if(tim>R[j]) break;
			if(j==n) dp[i]=0;
			if(L[j+1]>tim) chkmin(dp[i],dp[j+1]+tot);
		}
	}
	printf("%lld\n",dp[1]>1e14?-1:dp[1]+sum);
}

int main() {
	int T=1;
	// IN(T);
	while(T--) solve();
	return 0;
}


/*++++++++++++++++++++++++++++++
 File Name: code.cpp
 Author: Moonlights
 Created Time: 2020年11月04日 星期三 00时01分10秒
++++++++++++++++++++++++++++++*/

/*CYJian yyds!*/
/*ldx gg txdy!*/

// {{{ Template
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>

typedef std::vector <int> poly;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

#define pb push_back
#define pp pop_back
#define fi first
#define se second
#define rez resize
#define mkp std::make_pair
#define pii std::pair <int,int>
#define lep(i,l,r) for(int i=l;i<=r;++i)
#define rep(i,r,l) for(int i=r;i>=l;--i)
#define CLEAR(x) memset((x),0,sizeof((x)))
#define COPY(x,y) memcpy((x),(y),sizeof(x))

const int inf=1e9+7;
const int mod=998244353;

namespace __________ {
	namespace _in {
		template <class T> inline T read() {
			char ch;bool flag=0;T x=0;
			while(ch=getchar(),!isdigit(ch)) if(ch=='-') flag=1;
			while(isdigit(ch)) x=x*10+ch-48,ch=getchar();
			return flag?-x:x;
		}
		struct {
			inline operator ll() {return read<ll>();}
			inline operator int() {return read<int>();}
			inline operator bool() {return read<bool>();}
			template <class T> inline void operator() (T &x) {x=*this;}
			template <class T,class ...A> inline void operator() (T &x,A &...a)
			{x=*this,this->operator()(a...);}
		} IN;
	}
	namespace _std {
		template <class T> inline T abs(T x) {if(x<0) x=-x;return x;}
		template <class T> inline T max(T x,T y) {if(x<y) x=y;return x;}
		template <class T> inline T min(T x,T y) {if(x>y) x=y;return x;}
		template <class T> inline void swap(T &x,T &y) {x^=y^=x^=y;}
		template <class T> inline void chkmax(T&x,T y) {if(x<y) x=y;}
		template <class T> inline void chkmin(T&x,T y) {if(x>y) x=y;}
	}
	namespace _mod {
		inline int mul(int x,int y) {return 1ll*x*y%mod;}
		inline int dec(int x,int y) {x-=y;if(x<0) x+=mod;return x;}
		inline int add(int x,int y) {x+=y;if(x>=mod) x-=mod;return x;}
		inline void pls(int&x,int y) {x+=y;if(x>=mod) x-=mod;}
		inline void sub(int&x,int y) {x-=y;if(x<0) x+=mod;}
		inline int modpow(int x,int y,int res=1) {
			if(y==-1) y=mod-2;
			for(;y;y>>=1,x=mul(x,x)) if(y&1) res=mul(res,x);
			return res;
		}
	}

	using namespace _in;
	using namespace _std;
	using namespace _mod;
} using namespace __________;
// }}}

const int N=2e3+5;
int n,k,L[N],R[N],a[N],g[N][N];
ll f[N],sum[N],h[N][N];

inline void init_g() {
	lep(l,1,n) {
 		ll tim=L[l]+a[l]/k+((a[l]%k)>0)-1,tot=k-(a[l]%k),now;
		g[l][l]=(tim<=R[l]),h[l][l]=tim;

		lep(r,l+1,n) {
			if(a[r]<tot) tot-=a[r],++tim;
			else if(a[r]==tot) tot=k,++tim;
			else now=a[r]-tot,tim=max(tim+1,(ll)L[r])+now/k+((now%k)>0),tot=k-(now%k);
			g[l][r]=g[l][r-1]&(tim<=R[r]),h[l][r]=tim;
		}
	}
}

inline void DP() {
	f[1]=0;
	ll ans=g[1][n]?sum[n]:1e18;

	lep(i,2,n) {
		f[i]=1e18;
		lep(j,1,i-1)
			if(g[j][i-1]&&L[i]-h[j][i-1]>0)
				chkmin(f[i],f[j]+(sum[i-1]-sum[j-1]+k-1)/k*k);
		if(g[i][n]) chkmin(ans,f[i]+sum[n]-sum[i-1]);
	}
	printf("%lld\n",ans>1e12?-1:ans);
}

inline void solve() {
	IN(n,k);
	lep(i,1,n) IN(L[i],R[i],a[i]),sum[i]=sum[i-1]+a[i];
	init_g(),DP();
}

int main() {
	int T=1;
	// IN(T);
	while(T--) solve();
	return 0;
}


/*++++++++++++++++++++++++++++++
 File Name: code.cpp /jk
 Author: Moonlights
 Created Time: 2020年11月04日 星期三 00时01分10秒
++++++++++++++++++++++++++++++*/

/*CYJian yyds!*/
/*ldx gg txdy!*/

// {{{ Template
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>

typedef std::vector <int> poly;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

#define pb push_back
#define pp pop_back
#define fi first
#define se second
#define rez resize
#define mkp std::make_pair
#define pii std::pair <int,int>
#define lep(i,l,r) for(int i=l;i<=r;++i)
#define rep(i,r,l) for(int i=r;i>=l;--i)
#define CLEAR(x) memset((x),0,sizeof((x)))
#define COPY(x,y) memcpy((x),(y),sizeof(x))

const int inf=1e9+7;
const int mod=998244353;

namespace __________ {
	namespace _in {
		template <class T> inline T read() {
			char ch;bool flag=0;T x=0;
			while(ch=getchar(),!isdigit(ch)) if(ch=='-') flag=1;
			while(isdigit(ch)) x=x*10+ch-48,ch=getchar();
			return flag?-x:x;
		}
		struct {
			inline operator ll() {return read<ll>();}
			inline operator int() {return read<int>();}
			inline operator bool() {return read<bool>();}
			template <class T> inline void operator() (T &x) {x=*this;}
			template <class T,class ...A> inline void operator() (T &x,A &...a)
			{x=*this,this->operator()(a...);}
		} IN;
	}
	namespace _std {
		template <class T> inline T abs(T x) {if(x<0) x=-x;return x;}
		template <class T> inline T max(T x,T y) {if(x<y) x=y;return x;}
		template <class T> inline T min(T x,T y) {if(x>y) x=y;return x;}
		template <class T> inline void swap(T &x,T &y) {x^=y^=x^=y;}
		template <class T> inline void chkmax(T&x,T y) {if(x<y) x=y;}
		template <class T> inline void chkmin(T&x,T y) {if(x>y) x=y;}
	}
	namespace _mod {
		inline int mul(int x,int y) {return 1ll*x*y%mod;}
		inline int dec(int x,int y) {x-=y;if(x<0) x+=mod;return x;}
		inline int add(int x,int y) {x+=y;if(x>=mod) x-=mod;return x;}
		inline void pls(int&x,int y) {x+=y;if(x>=mod) x-=mod;}
		inline void sub(int&x,int y) {x-=y;if(x<0) x+=mod;}
		inline int modpow(int x,int y,int res=1) {
			if(y==-1) y=mod-2;
			for(;y;y>>=1,x=mul(x,x)) if(y&1) res=mul(res,x);
			return res;
		}
	}

	using namespace _in;
	using namespace _std;
	using namespace _mod;
} using namespace __________;
// }}}

const int N=2e3+5;
int n,k,L[N],R[N],a[N];
ll dp[N],sum;

inline void calc(int &tot,int p,int &tim) {
	int _,a=::a[p],L=::L[p];
	if(a<=tot) tot-=a,tim=L;
	else _=a-tot,tot=(k-(_%k))%k,tim=L+(_/k-(_%k==0))+1;
}
inline void solve() {
	IN(n,k);
	lep(i,1,n) IN(L[i],R[i],a[i]),sum+=a[i];

	dp[n+1]=0;
	rep(i,n,1) {
		dp[i]=1e18;

		int tot=k,tim;
		lep(j,i,n) {
			calc(tot,j,tim); if(tim>R[j]) break;
			if(j==n) dp[i]=0;
			if(L[j+1]>tim) chkmin(dp[i],dp[j+1]+tot);
		}
	}
	printf("%lld\n",dp[1]>1e14?-1:dp[1]+sum);
}

int main() {
	int T=1;
	// IN(T);
	while(T--) solve();
	return 0;
}


/*++++++++++++++++++++++++++++++
 File Name: code.cpp
 Author: Moonlights
 Created Time: 2020年11月04日 星期三 00时01分10秒
++++++++++++++++++++++++++++++*/

/*CYJian yyds!*/
/*ldx gg txdy!*/

// {{{ Template
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>

typedef std::vector <int> poly;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

#define pb push_back
#define pp pop_back
#define fi first
#define se second
#define rez resize
#define mkp std::make_pair
#define pii std::pair <int,int>
#define lep(i,l,r) for(int i=l;i<=r;++i)
#define rep(i,r,l) for(int i=r;i>=l;--i)
#define CLEAR(x) memset((x),0,sizeof((x)))
#define COPY(x,y) memcpy((x),(y),sizeof(x))

const int inf=1e9+7;
const int mod=998244353;

namespace __________ {
	namespace _in {
		template <class T> inline T read() {
			char ch;bool flag=0;T x=0;
			while(ch=getchar(),!isdigit(ch)) if(ch=='-') flag=1;
			while(isdigit(ch)) x=x*10+ch-48,ch=getchar();
			return flag?-x:x;
		}
		struct {
			inline operator ll() {return read<ll>();}
			inline operator int() {return read<int>();}
			inline operator bool() {return read<bool>();}
			template <class T> inline void operator() (T &x) {x=*this;}
			template <class T,class ...A> inline void operator() (T &x,A &...a)
			{x=*this,this->operator()(a...);}
		} IN;
	}
	namespace _std {
		template <class T> inline T abs(T x) {if(x<0) x=-x;return x;}
		template <class T> inline T max(T x,T y) {if(x<y) x=y;return x;}
		template <class T> inline T min(T x,T y) {if(x>y) x=y;return x;}
		template <class T> inline void swap(T &x,T &y) {x^=y^=x^=y;}
		template <class T> inline void chkmax(T&x,T y) {if(x<y) x=y;}
		template <class T> inline void chkmin(T&x,T y) {if(x>y) x=y;}
	}
	namespace _mod {
		inline int mul(int x,int y) {return 1ll*x*y%mod;}
		inline int dec(int x,int y) {x-=y;if(x<0) x+=mod;return x;}
		inline int add(int x,int y) {x+=y;if(x>=mod) x-=mod;return x;}
		inline void pls(int&x,int y) {x+=y;if(x>=mod) x-=mod;}
		inline void sub(int&x,int y) {x-=y;if(x<0) x+=mod;}
		inline int modpow(int x,int y,int res=1) {
			if(y==-1) y=mod-2;
			for(;y;y>>=1,x=mul(x,x)) if(y&1) res=mul(res,x);
			return res;
		}
	}

	using namespace _in;
	using namespace _std;
	using namespace _mod;
} using namespace __________;
// }}}

const int N=2e3+5;
int n,k,L[N],R[N],a[N];
ll dp[N],sum;

inline void calc(int &tot,int p,int &tim) {
	int _,a=::a[p],L=::L[p];
	if(a<=tot) tot-=a,tim=L;
	else _=a-tot,tot=(k-(_%k))%k,tim=L+(_/k-(_%k==0))+1;
}
inline void solve() {
	IN(n,k);
	lep(i,1,n) IN(L[i],R[i],a[i]),sum+=a[i];

	dp[n+1]=0;
	rep(i,n,1) {
		dp[i]=1e18;

		int tot=k,tim;
		lep(j,i,n) {
			calc(tot,j,tim); if(tim>R[j]) break;
			if(j==n) dp[i]=0;
			if(L[j+1]>tim+1) chkmin(dp[i],dp[j+1]+tot);
		}
	}
	printf("%lld\n",dp[1]>1e14?-1:dp[1]+sum);
}

int main() {
	int T=1;
	// IN(T);
	while(T--) solve();
	return 0;
}


/*++++++++++++++++++++++++++++++
 File Name: code.cpp
 Author: Moonlights
 Created Time: 2020年11月04日 星期三 00时01分10秒
++++++++++++++++++++++++++++++*/

/*CYJian yyds!*/
/*ldx gg txdy!*/

// {{{ Template
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>

typedef std::vector <int> poly;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

#define pb push_back
#define pp pop_back
#define fi first
#define se second
#define rez resize
#define mkp std::make_pair
#define pii std::pair <int,int>
#define lep(i,l,r) for(int i=l;i<=r;++i)
#define rep(i,r,l) for(int i=r;i>=l;--i)
#define CLEAR(x) memset((x),0,sizeof((x)))
#define COPY(x,y) memcpy((x),(y),sizeof(x))

const int inf=1e9+7;
const int mod=998244353;

namespace __________ {
	namespace _in {
		template <class T> inline T read() {
			char ch;bool flag=0;T x=0;
			while(ch=getchar(),!isdigit(ch)) if(ch=='-') flag=1;
			while(isdigit(ch)) x=x*10+ch-48,ch=getchar();
			return flag?-x:x;
		}
		struct {
			inline operator ll() {return read<ll>();}
			inline operator int() {return read<int>();}
			inline operator bool() {return read<bool>();}
			template <class T> inline void operator() (T &x) {x=*this;}
			template <class T,class ...A> inline void operator() (T &x,A &...a)
			{x=*this,this->operator()(a...);}
		} IN;
	}
	namespace _std {
		template <class T> inline T abs(T x) {if(x<0) x=-x;return x;}
		template <class T> inline T max(T x,T y) {if(x<y) x=y;return x;}
		template <class T> inline T min(T x,T y) {if(x>y) x=y;return x;}
		template <class T> inline void swap(T &x,T &y) {x^=y^=x^=y;}
		template <class T> inline void chkmax(T&x,T y) {if(x<y) x=y;}
		template <class T> inline void chkmin(T&x,T y) {if(x>y) x=y;}
	}
	namespace _mod {
		inline int mul(int x,int y) {return 1ll*x*y%mod;}
		inline int dec(int x,int y) {x-=y;if(x<0) x+=mod;return x;}
		inline int add(int x,int y) {x+=y;if(x>=mod) x-=mod;return x;}
		inline void pls(int&x,int y) {x+=y;if(x>=mod) x-=mod;}
		inline void sub(int&x,int y) {x-=y;if(x<0) x+=mod;}
		inline int modpow(int x,int y,int res=1) {
			if(y==-1) y=mod-2;
			for(;y;y>>=1,x=mul(x,x)) if(y&1) res=mul(res,x);
			return res;
		}
	}

	using namespace _in;
	using namespace _std;
	using namespace _mod;
} using namespace __________;
// }}}

const int N=2e3+5;
int n,k,L[N],R[N],a[N];
ll dp[N],sum;

inline void calc(int &tot,int p,int &tim) {
	int _,a=::a[p],L=::L[p];
	if(a<=tot) tot-=a,tim=L;
	else _=a-tot,tot=k-(_%k),tim=L+(_/k-(_%k==0))+1;
}
inline void solve() {
	IN(n,k);
	lep(i,1,n) IN(L[i],R[i],a[i]),sum+=a[i];

	dp[n+1]=0;
	rep(i,n,1) {
		dp[i]=1e18;

		int tot=k,tim;
		lep(j,i,n) {
			calc(tot,j,tim); if(tim>R[j]) break;
			if(j==n) dp[i]=0;
			if(L[j+1]>tim+1) chkmin(dp[i],dp[j+1]+tot);
		}
	}
	printf("%lld\n",dp[1]>1e14?-1:dp[1]+sum);
}

int main() {
	int T=1;
	// IN(T);
	while(T--) solve();
	return 0;
}


#include <cstdio>
typedef long long ll;
const int Maxn=2000;
int n;
ll k;
ll l[Maxn+5],r[Maxn+5],a[Maxn+5];
ll dp[Maxn+5];
int main(){
	scanf("%d%lld",&n,&k);
	for(int i=1;i<=n;i++){
		scanf("%lld%lld%lld",&l[i],&r[i],&a[i]);
	}
	for(int i=n;i>0;i--){
		ll need=a[i]+(i<n&&r[i]==l[i+1]?dp[i+1]:0);
		if((r[i]-l[i]+1)*k<need){
			puts("-1");
			return 0;
		}
		dp[i]=need-1ll*(r[i]-l[i])*k;
		if(dp[i]<0){
			dp[i]=0;
		}
	}
	ll ans=0,cur=k;
	for(int i=1;i<=n;i++){
		if(cur<dp[i]){
			ans+=cur;
			cur=k;
		}
		ans+=a[i];
		cur=((cur-a[i])%k+k)%k;
	}
	printf("%lld\n",ans);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const long long INF=1e18,N=5000;
int l[N],r[N],w[N];
int n,K;
long long f[N];
int main()
{
    scanf("%d%d",&n,&K);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d%d",l+i,r+i,w+i);
        if(1ll*(r[i]-l[i]+1)*K<w[i]){puts("-1");return 0;}
    }
    l[n+1]=r[n]+1;
    for(int i=1;i<=n;i++)f[i]=INF;
    for(int i=n;i>=1;i--)
    {
        int res=K,j;long long presum=0;
        for(j=i+1;j<=n+1;j++)
        {
            if(res+1ll*(r[j-1]-l[j-1])*K<w[j-1])break;
            res=(1ll*max(0ll,(0ll+w[j-1]-res+K-1)/K)*K+res)-w[j-1];
            presum+=w[j-1];if(j>n)res=0;
            if(l[j]!=r[j-1])f[i]=min(f[i],res+presum+f[j]);
        }
    }
    if(f[1]==INF)puts("-1");
    else cout<<f[1]<<endl;

}


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const long long INF=1e18,N=5000;
int l[N],r[N],w[N];
int n,K;
long long f[N];
int main()
{
    scanf("%d%d",&n,&K);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d%d",l+i,r+i,w+i);
        if(1ll*(r[i]-l[i]+1)*K<w[i]){puts("-1");return 0;}
    }
    l[n+1]=r[n]+1;
    for(int i=1;i<=n;i++)f[i]=INF;
    for(int i=n;i>=1;i--)
    {
        int res=K,j;long long presum=0;
        for(j=i+1;j<=n+1;j++)
        {
            if(res+1ll*(r[j-1]-l[j-1])*K<w[j-1])break;
            int t=max(0ll,(0ll+w[j-1]-res+K-1)/K);
            res=(1ll*t*K+res)-w[j-1];
            presum+=w[j-1];if(j>n)res=0;
            if(l[j]!=r[j-1]||t<r[j-1]-l[j-1])f[i]=min(f[i],res+presum+f[j]);
        }
    }
    if(f[1]==INF)puts("-1");
    else cout<<f[1]<<endl;

}


#include<bits/stdc++.h>
#define dd(x) cout<<#x<<" = "<<x<<" "
#define de(x) cout<<#x<<" = "<<x<<endl
#define de_arr(a,s,t) cout<<#a<<": ";for (int z=(s);z<=(t);++z)cout<<a[z]<<" ";cout<<endl;
#define sz(x) int(x.size())
#define All(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
const double eps=1e-8;
inline int sign(double x){return (x>eps)-(x<-eps);}
const int maxn=3e5+10,mod=1e9+7,INF=0x3f3f3f3f;
int l[maxn],r[maxn],a[maxn];
int f[maxn];
int main()
{
	int n,k;
	cin>>n>>k;
	for (int i=1;i<=n;++i)
		scanf("%d%d%d",&l[i],&r[i],&a[i]);
	for (int i=n;i;--i){
		int need=a[i]+(r[i]==l[i+1]?f[i+1]:0);
		if (need>ll(r[i]-l[i]+1)*k){
			cout<<-1;
			return 0;
		}
		f[i]=max(0ll,need-ll(r[i]-l[i])*k);
	}
	ll ans=0,now=k;
	for (int i=1;i<=n;++i){
		if (now<f[i]){
			ans+=now;
			now=k;
		}
		ans+=a[i];
		now-=a[i]%k;
		if (now<0)	now+=k;
	}
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>

#define fi first
#define se second
#define db double
#define U unsigned
#define P std::pair<int,int>
#define LL long long
#define pb push_back
#define MP std::make_pair
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl

const int MAXN = 2000+5;

int n;LL k;
LL l[MAXN],r[MAXN],a[MAXN];
LL f[MAXN];

int main(){
	scanf("%d%lld",&n,&k);
	FOR(i,1,n) scanf("%lld%lld%lld",l+i,r+i,a+i);
	ROF(i,n,1){
		int x = a[i];
		if(r[i] == l[i+1]) x += f[i+1];
		if(x > (r[i]-l[i]+1)*k){
			puts("-1");
			return 0;
		}
		f[i] = std::max(0ll,x-(r[i]-l[i])*k);
	}
	LL ans = 0,now = k;
	FOR(i,1,n){
		if(now < f[i]){
			ans += now;
			now = k;
		}
		ans += a[i];
		now = (now-a[i]%k+k)%k;
	}
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int maxn=2E3+5;
const ll inf=1E18;
ll n,k,a[maxn],L[maxn],R[maxn],f[maxn];
int main()
{
	ios::sync_with_stdio(false);
	cin>>n>>k;
	for(int i=1;i<=n;++i)
	{
		cin>>L[i]>>R[i]>>a[i];
		f[i]=inf;
	}
	ll ans=inf;
	for(int i=0;i<n;++i)
	{
		ll now=k,sum=f[i];
		for(int j=i+1;j<=n;++j)
		{
			ll times=(a[j]-now-1)/k+1;
			if(a[j]<=now)
				times=0;
			if(times>R[j]-L[j])
				break;
			now=now+k*times-a[j];
			sum+=a[j];
			if(L[j]+times<R[j]||L[j+1]!=L[j])
				f[j]=min(f[j],sum+now);
			if(j==n)
				ans=min(ans,sum);
		}
	}
	if(ans<inf)
		cout<<ans<<endl;
	else
		cout<<-1<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int maxn=2E3+5;
const ll inf=1E18;
ll n,k,a[maxn],L[maxn],R[maxn],f[maxn];
int main()
{
	ios::sync_with_stdio(false);
	cin>>n>>k;
	for(int i=1;i<=n;++i)
	{
		cin>>L[i]>>R[i]>>a[i];
		f[i]=inf;
	}
	ll ans=inf;
	for(int i=0;i<n;++i)
	{
		ll now=k,sum=f[i];
		for(int j=i+1;j<=n;++j)
		{
			ll times=(a[j]-now-1)/k+1;
			if(a[j]<=now)
				times=0;
			if(times>R[j]-L[j])
				break;
			now=now+k*times-a[j];
			sum+=a[j];
			if(L[j]+times<R[j])
				f[j]=min(f[j],sum+now);
			if(j==n)
				ans=min(ans,sum);
		}
	}
	if(ans<inf)
		cout<<ans<<endl;
	else
		cout<<-1<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int maxn=2E3+5;
const ll inf=1E18;
ll n,k,a[maxn],L[maxn],R[maxn],f[maxn];
int main()
{
	ios::sync_with_stdio(false);
	cin>>n>>k;
	for(int i=1;i<=n;++i)
	{
		cin>>L[i]>>R[i]>>a[i];
		f[i]=inf;
	}
	ll ans=inf;
	for(int i=0;i<n;++i)
	{
		ll now=k,sum=f[i];
		for(int j=i+1;j<=n;++j)
		{
			ll times=(a[j]-now-1)/k+1;
			if(a[j]<=now)
				times=0;
			if(times>R[j]-L[j])
				break;
			now=now+k*times-a[j];
			sum+=a[j];
			if(L[j]+times<R[j]||L[j]+times!=L[j+1])
				f[j]=min(f[j],sum+now);
			if(j==n)
				ans=min(ans,sum);
		}
	}
	if(ans<inf)
		cout<<ans<<endl;
	else
		cout<<-1<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int maxn=2E3+5;
const ll inf=1E18;
ll n,k,a[maxn],L[maxn],R[maxn],f[maxn];
int main()
{
	ios::sync_with_stdio(false);
	cin>>n>>k;
	for(int i=1;i<=n;++i)
	{
		cin>>L[i]>>R[i]>>a[i];
		f[i]=inf;
	}
	ll ans=inf;
	for(int i=0;i<n;++i)
	{
		ll now=k,sum=f[i];
		for(int j=i+1;j<=n;++j)
		{
			ll times=(a[j]-now-1)/k+1;
			if(a[j]<=now)
				times=0;
			if(times>R[j]-L[j])
				break;
			now=now+k*times-a[j];
			sum+=a[j];
			if(L[j]+times<R[j]||L[j]+times!=L[j])
				f[j]=min(f[j],sum+now);
			if(j==n)
				ans=min(ans,sum);
		}
	}
	if(ans<inf)
		cout<<ans<<endl;
	else
		cout<<-1<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int maxn=2E3+5;
const ll inf=1E18;
ll n,k,a[maxn],L[maxn],R[maxn],f[maxn];
int main()
{
	ios::sync_with_stdio(false);
	cin>>n>>k;
	for(int i=1;i<=n;++i)
	{
		cin>>L[i]>>R[i]>>a[i];
		f[i]=inf;
	}
	f[1]=0;
	ll ans=inf;
	for(int i=1;i<=n;++i)
	{
		ll now=k,sum=f[i];
		for(int j=i;j<=n;++j)
		{
			ll times=(a[j]-now-1)/k+1;
			if(a[j]<=now)
				times=0;
			if(times>R[j]-L[j])
				break;
			now=now+times*k-a[j];
			sum+=a[j];
			if(j!=n)
			{
				if(L[j]+times<L[j+1])
					f[j+1]=min(f[j+1],sum+now);
			}
			else
				ans=min(ans,sum);
		}
	}
	if(ans<inf)
		cout<<ans<<endl;
	else
		cout<<-1<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int maxn=2E3+5;
const ll inf=1E18;
ll n,k,a[maxn],L[maxn],R[maxn],f[maxn];
int main()
{
	ios::sync_with_stdio(false);
	cin>>n>>k;
	for(int i=1;i<=n;++i)
	{
		cin>>L[i]>>R[i]>>a[i];
		f[i]=inf;
	}
	ll ans=inf;
	for(int i=0;i<n;++i)
	{
		ll now=k,sum=f[i];
		for(int j=i+1;j<=n;++j)
		{
			ll times=(a[j]-now-1)/k+1;
			if(a[j]<=now)
				times=0;
			if(times>R[j]-L[j])
				break;
			now=now+k*times-a[j];
			sum+=a[j];
			if(L[j]+times!=L[j+1])
				f[j]=min(f[j],sum+now);
			if(j==n)
				ans=min(ans,sum);
		}
	}
	if(ans<inf)
		cout<<ans<<endl;
	else
		cout<<-1<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int maxn=2E3+5;
const ll inf=1E18;
ll n,k,a[maxn],L[maxn],R[maxn],f[maxn];
int main()
{
	ios::sync_with_stdio(false);
	cin>>n>>k;
	for(int i=1;i<=n;++i)
	{
		cin>>L[i]>>R[i]>>a[i];
		f[i]=inf;
	}
	f[1]=0;
	ll ans=inf;
	for(int i=1;i<=n;++i)
	{
		ll now=k,sum=f[i];
		for(int j=i;j<=n;++j)
		{
			ll times=max((ll)0,(a[j]-now-1)/k+1);
			if(times>R[j]-L[j])
				break;
			now=now+times*k-a[j];
			sum+=a[j];
			if(j!=n)
			{
				if(L[j]+times<L[j+1])
					f[j+1]=min(f[j+1],sum+now);
			}
			else
				ans=min(ans,sum);
		}
	}
	if(ans<inf)
		cout<<ans<<endl;
	else
		cout<<-1<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
void read (int &x) {
    char ch = getchar(); x = 0;
    while (!isdigit(ch)) ch = getchar();
    while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
} const int N = 2048, inf = 1e15;
int n, k, l[N], r[N], a[N], f[N];
signed main() {
    read (n), read (k);
    for (int i = 1; i <= n; ++i)
        read (l[i]), read (r[i]), read (a[i]);
    for (int i = n; i >= 1; --i) {
        int num = a[i] + (r[i] == l[i + 1]) * f[i + 1];
        if (num > (r[i] - l[i] + 1) * k) return puts ("-1"), 0;
        f[i] = max (0ll, num - (r[i] - l[i]) * k);
    } int res = 0, now = 0;
    for (int i = 1; i <= n; ++i) {
        if (now < f[i]) res += now, now = k;
        res += a[i], now = ((now - a[i]) % k + k) % k;
    }
    return printf ("%lld\n", res), 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
const int N = 2010;
int l[N], r[N], a[N], dp[N];
int n, k;

int main() {
    cin >> n >>  k;
    for (int i = 1; i <= n; i++)
        cin >> l[i] >> r[i] >> a[i];
    for (int i = n; i >= 1; i--) {
        LL need = a[i];
        if (i + 1 <= n && r[i] == l[i + 1])
            need += dp[i + 1];
        if (k * (r[i] - l[i] + 1) < need) {
            puts("-1");
            return 0;
        }
        dp[i] = max(0ll, need - k * (r[i] - l[i]));
    }

    LL ans = 0, remain = k;
    for (int i = 1; i <= n; i++) {
        if (remain < dp[i]) {
            ans += remain;
            remain = k;
        }
        ans += a[i];
        remain = ((remain - a[i]) % k + k) % k;
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
const int N = 2010;
LL l[N], r[N], a[N], dp[N];
int n, k;

int main() {
    cin >> n >>  k;
    for (int i = 1; i <= n; i++)
        cin >> l[i] >> r[i] >> a[i];
    for (int i = n; i >= 1; i--) {
        LL need = a[i];
        if (i + 1 <= n && r[i] == l[i + 1])
            need += dp[i + 1];
        if (k * (r[i] - l[i] + 1) < need) {
            puts("-1");
            return 0;
        }
        dp[i] = max(0ll, need - k * (r[i] - l[i]));
    }

    LL ans = 0, remain = k;
    for (int i = 1; i <= n; i++) {
        if (remain < dp[i]) {
            ans += remain;
            remain = k;
        }
        ans += a[i];
        remain = ((remain - a[i]) % k + k) % k;
    }
    cout << ans << endl;
    return 0;
}


/*
 _      _  _ _ _ _ _   _      _
\_\   /_/ \_\_\_\_\_\ \_\   /_/
 \_\ /_/      \_\      \_\ /_/
  \_\_/       \_\       \_\_/
   \_\         \_\       \_\
   \_\     \_\ \_\       \_\
   \_\      \_\\_\       \_\
*/
#include <bits/stdc++.h>
#define rep(i,x,y) for(int i=x;i<=y;i++)
#define per(i,x,y) for(int i=x;i>=y;i--)
#define sz(v) (int)v.size()
using namespace std;
template<class T>
void read(T&x) {
	static int c,neg;
	x=neg=0;c=getchar();
	while(!isdigit(c)){if(c=='-')neg=1;c=getchar();}
	while(isdigit(c)){x=x*10+c-48;c=getchar();}if(neg)x=-x;
}
template<class T>
void print(T x,char c='\n'){
	static int st[233],top;
	if(x<0)putchar('-'),x=-x;
	do{st[++top]=x%10;x/=10;}while(x);
	while(top)putchar(st[top--]+48);putchar(c);
}
template<class T>
void cmax(T&x,const T&y){
	x=(x>y)?x:y;
}
template<class T>
void cmin(T&x,const T&y){
	x=(x<y)?x:y;
}
int n,k;
const int N=2e3+5;
int l[N],r[N],a[N];
long long f[N];
int main(){
	read(n);
	read(k);
	rep(i,1,n){
		read(l[i]);
		read(r[i]);
		read(a[i]);
	}
	per(i,n,1){
		long long ned=a[i];
		if(i<n&&r[i]==l[i+1]){
			ned+=f[i+1];
		}
		if(1ll*(r[i]-l[i]+1)*k<ned){
			print(-1);
			exit(0);
		}
		f[i]=ned-1ll*(r[i]-l[i])*k;
		if(f[i]<0){
			f[i]=0;
		}
	}
	long long ans=0,cur=k;
	rep(i,1,n)ans+=a[i];
	rep(i,1,n){
		if(cur<f[i]){
			ans+=cur;
			cur=k;
		}
		cur-=a[i];
		cur%=k;
		if(cur<0)cur+=k;
	}
	print(ans);
	return 0;
}


/*
 _      _  _ _ _ _ _   _      _
\_\   /_/ \_\_\_\_\_\ \_\   /_/
 \_\ /_/      \_\      \_\ /_/
  \_\_/       \_\       \_\_/
   \_\         \_\       \_\
   \_\     \_\ \_\       \_\
   \_\      \_\\_\       \_\
*/
#include <bits/stdc++.h>
#define rep(i,x,y) for(int i=x;i<=y;i++)
#define per(i,x,y) for(int i=x;i>=y;i--)
#define sz(v) (int)v.size()
using namespace std;
template<class T>
void read(T&x) {
	static int c,neg;
	x=neg=0;c=getchar();
	while(!isdigit(c)){if(c=='-')neg=1;c=getchar();}
	while(isdigit(c)){x=x*10+c-48;c=getchar();}if(neg)x=-x;
}
template<class T>
void print(T x,char c='\n'){
	static int st[233],top;
	if(x<0)putchar('-'),x=-x;
	do{st[++top]=x%10;x/=10;}while(x);
	while(top)putchar(st[top--]+48);putchar(c);
}
template<class T>
void cmax(T&x,const T&y){
	x=(x>y)?x:y;
}
template<class T>
void cmin(T&x,const T&y){
	x=(x<y)?x:y;
}
int n,k;
const int N=2e3+5;
int l[N],r[N],a[N];
long long f[N];
int main(){
	read(n);
	read(k);
	rep(i,1,n){
		read(l[i]);
		read(r[i]);
		read(a[i]);
	}
	per(i,n,1){
		long long ned=a[i];
		if(i<n&&r[i]==l[i+1]){
			ned+=f[i+1];
		}
		if(1ll*(r[i]-l[i]+1)*k<ned){
			print(-1);
			exit(0);
		}
		f[i]=ned-(r[i]-l[i])*k;
		if(f[i]<0){
			f[i]=0;
		}
	}
	long long ans=0,cur=k;
	rep(i,1,n)ans+=a[i];
	rep(i,1,n){
		if(cur<f[i]){
			ans+=cur;
			cur=k;
		}
		cur-=a[i];
		cur%=k;
		if(cur<0)cur+=k;
	}
	print(ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define mod 1000000007
#define mod2 998244353
#define pi pair<ll,ll>
#define pii pair<ll,pair<ll,ll>>
#define vi vector<ll>
#define pb push_back
#define ld long double
#define vpi vector<pair<ll,ll>>
#define vvi vector<vector<ll>>

#define debug(a) { cout << #a << " = " << a << endl;}

void sep() {
    cout << "-------------BLOCK-----------------\n";
}

template<typename T>
void debugv(vector<T> arr) {
    for (auto t: arr)
        cout << t << " ";
    cout << "\n";
}

template<typename T>
void debugarr(T arr, int n) {
    for (int i=0;i<n;i++) cout << arr[i] << " ";
    cout << "\n";
}

ll __min(ll a, ll b) {
    return a < b ? a : b;
}

ll __max(ll a, ll b) {
    return a > b ? a : b;
}

const int maxn = 2e3+100;
ll n, m, t, k, q;
unordered_map<ll,ll> u;
ll l[maxn],r[maxn],w[maxn];
ll rdp[maxn];
ll nrdp[maxn];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin>>n>>k;
    for (int i=0;i<n;i++) {
        cin>>l[i]>>r[i]>>w[i];
        rdp[i]=INT_MAX;
        nrdp[i]=INT_MAX;
    }

    ll rl=w[0]%k;
    ll nrl=w[0]%k;
    rdp[0]=w[0];
    nrdp[0]=w[0];
    bool pos=true;

    for (int i=0;i<n-1;i++) {
        if ((r[i+1]-l[i+1]+1)*k<w[i+1]) {
            pos=false;
            break;
        }
        ll nrl_contrib = (k - nrl)%k;
        nrl_contrib = (nrl_contrib +k)%k;
        ll rl_contrib = (k - rl);
        rl_contrib = (rl_contrib +k)%k;
        if (r[i+1]>l[i+1])
            rdp[i+1]=__min(nrl_contrib+w[i+1]+nrdp[i],rl_contrib+w[i+1]+rdp[i]);

        ll b = (w[i+1]-rl)%k;
        b = (b+k)%k;
        b += rdp[i];
        if (w[i+1]<=rl) {
            ll ctt = rl - w[i+1] + rdp[i];
            if (nrdp[i+1] > ctt) {
                nrl = 0;
                nrdp[i+1] = ctt;
            }
        } else {
            ll tnrl = w[i+1] - rl;
            ll turns = (tnrl + k - 1)%k;
            if (turns < r[i+1]-l[i+1]) {
                nrdp[i+1]=b;
                nrl = tnrl%k;
                nrl = (nrl+k)%k;
            }
        }

        ll a = (w[i+1]-nrl)%k;
        a = (a+k)%k;
        a += nrdp[i];
        if (w[i+1]<=nrl) {
            ll ctt = nrl - w[i+1] + nrdp[i];
            if (nrdp[i+1] > ctt) {
                nrl = 0;
                nrdp[i+1] = ctt;
            }
        } else {

            ll tnrl = w[i+1] - nrl;
            ll turns = (tnrl + k - 1)%k;
            if (turns < r[i+1]-l[i+1] && nrdp[i+1] > a) {
                nrdp[i+1] = a;
                nrl = tnrl%k;
                nrl = (nrl+k)%k;
            }
        }
        rl=w[i+1]%k;
    }

    ll ans= __min(nrdp[n-1],rdp[n-1]);
    if (!pos || ans >= INT_MAX) cout << -1 << endl;
    else cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define mod 1000000007
#define mod2 998244353
#define pi pair<ll,ll>
#define pii pair<ll,pair<ll,ll>>
#define vi vector<ll>
#define pb push_back
#define ld long double
#define vpi vector<pair<ll,ll>>

#define debug(a) { cout << #a << " = " << a << endl;}

void sep() {
    cout << "-------------BLOCK-----------------\n";
}

template<typename T>
void debugv(vector<T> arr) {
    for (auto t: arr)
        cout << t << " ";
    cout << "\n";
}

template<typename T>
void debugarr(T arr, int n) {
    for (int i=0;i<n;i++) cout << arr[i] << " ";
    cout << "\n";
}

ll __min(ll a, ll b) {
    return a < b ? a : b;
}

ll __max(ll a, ll b) {
    return a > b ? a : b;
}

const int maxn = 4e6+100;
ll n, m, t, k, q;
unordered_map<ll,ll> u;

typedef struct node {
    ll l;
    ll r;
    ll num;
    ll req;
    ll fin;
    node(ll left,ll right, ll n): l(left),r(right),num(n),req(0),fin(0) {}
} node;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin>>n>>k;
    vector<node*> arr;
    ll a,b,c;
    for (int i=0;i<n;i++) {
        cin>>a>>b>>c;
        arr.pb(new node(a,b,c));
    }
    bool pos=true;
    for (int i=0;i<n;i++) {
        node* curr=arr[i];
        ll tc=curr->r-curr->l + 1;
        ll a=(curr->num/k);
        a+=curr->num%k?1:0;
        arr[i]->fin=a;
        if (tc < a) {
            pos=false;
            break;
        }
        if (tc==a) {
            if (curr->num%k) arr[i]->req=curr->num%k;
            else arr[i]->req=k;
        }
    }
    if (!pos) {
        cout << -1 << endl;
        exit(0);
    }
    ll ans=0;
    ll tb=0;
    ll prev=-1;
    for (int i=0;i<n;i++) {
        ll cstart=0;
        if (arr[i]->l==prev) {
            cstart++;
        }
        ll fin=arr[i]->fin+cstart+arr[i]->l-1;
        if (fin > arr[i]->r) {
            cout << -1 << endl;
            exit(0);
        }

        if (arr[i]->num>=tb) {
            ans+=(arr[i]->num-tb);
            tb=(arr[i]->num-tb)%k;
        } else {
            ans += tb-arr[i]->num;
            tb -= arr[i]->num;
        }

        if (fin < arr[i]->r) {
            fin=0;
            prev=-1;
            if (i+1<n&&arr[i+1]->req>tb) {
                ans += k-tb;
                tb=0;
            }
        } else {
            if (i+1<n&&arr[i+1]->req>tb) {
                cout << -1 << endl;
                exit(0);
            }
            prev=arr[i]->r;
        }
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define mod 1000000007
#define mod2 998244353
#define pi pair<ll,ll>
#define pii pair<ll,pair<ll,ll>>
#define vi vector<ll>
#define pb push_back
#define ld long double
#define vpi vector<pair<ll,ll>>
#define vvi vector<vector<ll>>

#define debug(a) { cout << #a << " = " << a << endl;}

void sep() {
    cout << "-------------BLOCK-----------------\n";
}

template<typename T>
void debugv(vector<T> arr) {
    for (auto t: arr)
        cout << t << " ";
    cout << "\n";
}

template<typename T>
void debugarr(T arr, int n) {
    for (int i=0;i<n;i++) cout << arr[i] << " ";
    cout << "\n";
}

ll __min(ll a, ll b) {
    return a < b ? a : b;
}

ll __max(ll a, ll b) {
    return a > b ? a : b;
}

const int maxn = 2e3+100;
ll n, m, t, k, q;
unordered_map<ll,ll> u;

ll l[maxn], r[maxn], w[maxn];
ll dp[maxn];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin>>n>>k;
    for (int i=0;i<n;i++) cin>>l[i]>>r[i]>>w[i];
    for (int i=0;i<n;i++) dp[i]=INT_MAX;
    dp[0]=0;
    ll ans=INT_MAX;
    for (int i=0;i<n;i++) {
        if (dp[i]<INT_MAX) {
            ll st=k;
            ll tot=dp[i];
            bool pos=true;
            for (int j=i;j<n;j++) {
                ll steps=(w[j]-st+k-1)/k;
                if (l[j]+steps>r[j]) {
                    pos=false;
                    break;
                }
                tot += w[j];
                st = (st-w[j])%k;
                st = (st + k)%k;

                if (j+1<n && l[i]+steps<l[i+1])
                    dp[j+1]=__min(dp[j+1],tot+st);
            }
            if (pos)
                ans=__min(ans,tot);
        }
    }
    if (ans>=INT_MAX) ans=-1;
    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define mod 1000000007
#define mod2 998244353
#define pi pair<ll,ll>
#define pii pair<ll,pair<ll,ll>>
#define vi vector<ll>
#define pb push_back
#define ld long double
#define vpi vector<pair<ll,ll>>
#define vvi vector<vector<ll>>

#define debug(a) { cout << #a << " = " << a << endl;}

void sep() {
    cout << "-------------BLOCK-----------------\n";
}

template<typename T>
void debugv(vector<T> arr) {
    for (auto t: arr)
        cout << t << " ";
    cout << "\n";
}

template<typename T>
void debugarr(T arr, int n) {
    for (int i=0;i<n;i++) cout << arr[i] << " ";
    cout << "\n";
}

ll __min(ll a, ll b) {
    return a < b ? a : b;
}

ll __max(ll a, ll b) {
    return a > b ? a : b;
}

const int maxn = 2e3+100;
ll n, m, t, k, q;
unordered_map<ll,ll> u;
ll l[maxn],r[maxn],w[maxn];
ll rdp[maxn];
ll nrdp[maxn];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin>>n>>k;
    for (int i=0;i<n;i++) {
        cin>>l[i]>>r[i]>>w[i];
        rdp[i]=INT_MAX;
        nrdp[i]=INT_MAX;
    }

    ll rl=w[0]%k;
    ll nrl=w[0]%k;
    rdp[0]=w[0];
    nrdp[0]=w[0];
    ll fturns = (w[0]+k-1)/k;
    bool pos=true;
    bool rlpos_act=fturns<r[0];
    bool nrlpos_act=fturns<r[0];

    for (int i=0;i<n-1;i++) {
        if ((r[i+1]-l[i+1]+1)*k<w[i+1]) {
            pos=false;
            break;
        }
        bool nrlpos=false;
        bool rlpos=false;
        ll nrl_contrib = (k - nrl)%k;
        nrl_contrib = (nrl_contrib +k)%k;
        ll rl_contrib = (k - rl);
        rl_contrib = (rl_contrib +k)%k;
        if (r[i+1]>l[i+1]) {
            ll a = nrl_contrib + w[i+1] + nrdp[i];
            ll b = rl_contrib + w[i+1] + rdp[i];
            ll tturns=(w[i+1]+k-1)/k;
            rlpos=tturns<r[i+1];
            if (nrlpos_act) {
                rdp[i+1] = __min(a,rdp[i+1]);
            } else if (rlpos_act) {
                rdp[i+1] = __min(b,rdp[i+1]);
            }
        }

        ll b = (w[i+1]-rl)%k;
        b = (b+k)%k;
        b += rdp[i];
        if (w[i+1]<=rl) {
            ll ctt = rl - w[i+1] + rdp[i];
            if (nrdp[i+1] > ctt) {
                nrl = 0;
                nrdp[i+1] = ctt;
                nrlpos=true;
            }
        } else {
            ll tnrl = w[i+1] - rl;
            ll turns = (tnrl + k - 1)%k;
            if (turns < r[i+1]-l[i+1]) {
                nrdp[i+1]=b;
                nrl = tnrl%k;
                nrl = (nrl+k)%k;
                nrlpos=(turns<r[i+1]);
            }
        }

        ll a = (w[i+1]-nrl)%k;
        a = (a+k)%k;
        a += nrdp[i];
        if (w[i+1]<=nrl) {
            ll ctt = nrl - w[i+1] + nrdp[i];
            if (nrdp[i+1] > ctt) {
                nrl = 0;
                nrdp[i+1] = ctt;
                nrlpos=true;
            }
        } else {

            ll tnrl = w[i+1] - nrl;
            ll turns = (tnrl + k - 1)%k;
            if (turns < r[i+1]-l[i+1] && nrdp[i+1] > a) {
                nrdp[i+1] = a;
                nrl = tnrl%k;
                nrl = (nrl+k)%k;
                nrlpos=(turns<r[i+1]);
            }
        }

        nrlpos_act=nrlpos;
        rlpos_act=rlpos;
    }

    ll ans= __min(nrdp[n-1],rdp[n-1]);
    if (!pos || ans >= INT_MAX) cout << -1 << endl;
    else cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define mod 1000000007
#define mod2 998244353
#define pi pair<ll,ll>
#define pii pair<ll,pair<ll,ll>>
#define vi vector<ll>
#define pb push_back
#define ld long double
#define vpi vector<pair<ll,ll>>
#define vvi vector<vector<ll>>

#define debug(a) { cout << #a << " = " << a << endl;}

void sep() {
    cout << "-------------BLOCK-----------------\n";
}

template<typename T>
void debugv(vector<T> arr) {
    for (auto t: arr)
        cout << t << " ";
    cout << "\n";
}

template<typename T>
void debugarr(T arr, int n) {
    for (int i=0;i<n;i++) cout << arr[i] << " ";
    cout << "\n";
}

ll __min(ll a, ll b) {
    return a < b ? a : b;
}

ll __max(ll a, ll b) {
    return a > b ? a : b;
}

const int maxn = 2e3+100;
ll n, m, t, k, q;
unordered_map<ll,ll> u;
ll l[maxn],r[maxn],w[maxn];
ll dp[maxn][maxn];

ll go(ll curr, ll last, ll prev) {
    if (curr == n) {
        return -prev;
    }
    if (dp[curr][last]<INT_MAX) return dp[curr][last];
    ll used = w[curr] - prev;
    ll reloads = (used + k - 1)/k;
    if (r[curr]-l[curr]<reloads) return dp[curr][last]=INT_MAX;
    used=reloads*k;
    ll left = used + prev - w[curr];
    ll ans = used + go(curr+1,last, left);
    if (curr+1<n&&l[curr]+reloads<l[curr+1])
        ans=__min(ans,used+k+go(curr+1,curr+1,k));
    return dp[curr][last]=ans;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin>>n>>k;
    for (int i=0;i<n;i++) cin>>l[i]>>r[i]>>w[i];
    for (int i=0;i<n+2;i++) {
        for (int j=0;j<n+2;j++) {
            dp[i][j]=INT_MAX;
        }
    }
    ll ans = k + go(0,0,k);
    if (ans >= INT_MAX) cout << -1 << endl;
    else cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define mod 1000000007
#define mod2 998244353
#define pi pair<ll,ll>
#define pii pair<ll,pair<ll,ll>>
#define vi vector<ll>
#define pb push_back
#define ld long double
#define vpi vector<pair<ll,ll>>
#define vvi vector<vector<ll>>

#define debug(a) { cout << #a << " = " << a << endl;}

void sep() {
    cout << "-------------BLOCK-----------------\n";
}

template<typename T>
void debugv(vector<T> arr) {
    for (auto t: arr)
        cout << t << " ";
    cout << "\n";
}

template<typename T>
void debugarr(T arr, int n) {
    for (int i=0;i<n;i++) cout << arr[i] << " ";
    cout << "\n";
}

ll __min(ll a, ll b) {
    return a < b ? a : b;
}

ll __max(ll a, ll b) {
    return a > b ? a : b;
}

const int maxn = 2e3+100;
ll n, m, t, k, q;
unordered_map<ll,ll> u;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin>>n>>k;
    ll l[n], r[n], w[n];
    ll dp[n];
    for (int i=0;i<n;i++) cin>>l[i]>>r[i]>>w[i];
    for (int i=0;i<n;i++) dp[i]=LLONG_MAX;
    dp[0]=0;
    ll ans=LLONG_MAX;
    for (ll i=0;i<n;i++) {
        if (dp[i]<LLONG_MAX) {
            ll run=k;
            ll tot=dp[i];
            bool pos=true;
            for (ll j=i;j<n;j++) {
                ll x=(w[j]-run+k-1)/k;
                ll p=l[j]+x;
                if (p>r[j]) {
                    pos=false;
                    break;
                }
                tot += w[j];
                run = (run-w[j])%k;
                run = (run + k)%k;

                if (j+1<n && p<l[j+1])
                    dp[j+1]=__min(dp[j+1],tot+run);
            }
            if (pos)
                ans=__min(ans,tot);
        }
    }
    if (ans>=LLONG_MAX) ans=-1;
    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define mod 1000000007
#define mod2 998244353
#define pi pair<ll,ll>
#define pii pair<ll,pair<ll,ll>>
#define vi vector<ll>
#define pb push_back
#define ld long double
#define vpi vector<pair<ll,ll>>
#define vvi vector<vector<ll>>

#define debug(a) { cout << #a << " = " << a << endl;}

void sep() {
    cout << "-------------BLOCK-----------------\n";
}

template<typename T>
void debugv(vector<T> arr) {
    for (auto t: arr)
        cout << t << " ";
    cout << "\n";
}

template<typename T>
void debugarr(T arr, int n) {
    for (int i=0;i<n;i++) cout << arr[i] << " ";
    cout << "\n";
}

ll __min(ll a, ll b) {
    return a < b ? a : b;
}

ll __max(ll a, ll b) {
    return a > b ? a : b;
}

const int maxn = 2e3+100;
ll n, m, t, k, q;
unordered_map<ll,ll> u;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin>>n>>k;
    ll l[n], r[n], w[n];
    ll dp[n];
    for (int i=0;i<n;i++) cin>>l[i]>>r[i]>>w[i];
    for (int i=0;i<n;i++) dp[i]=INT_MAX;
    dp[0]=0;
    ll ans=INT_MAX;
    for (ll i=0;i<n;i++) {
        if (dp[i]<INT_MAX) {
            ll run=k;
            ll tot=dp[i];
            bool pos=true;
            for (ll j=i;j<n;j++) {
                ll x=(w[j]-run+k-1)/k;
                ll p=l[j]+x;
                if (p>r[j]) {
                    pos=false;
                    break;
                }
                tot += w[j];
                run = (run-w[j])%k;
                run = (run + k)%k;

                if (j+1<n && p<l[j+1])
                    dp[j+1]=__min(dp[j+1],tot+run);
            }
            if (pos)
                ans=__min(ans,tot);
        }
    }
    if (ans>=INT_MAX) ans=-1;
    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define mod 1000000007
#define mod2 998244353
#define pi pair<ll,ll>
#define pii pair<ll,pair<ll,ll>>
#define vi vector<ll>
#define pb push_back
#define ld long double
#define vpi vector<pair<ll,ll>>
#define vvi vector<vector<ll>>

#define debug(a) { cout << #a << " = " << a << endl;}

void sep() {
    cout << "-------------BLOCK-----------------\n";
}

template<typename T>
void debugv(vector<T> arr) {
    for (auto t: arr)
        cout << t << " ";
    cout << "\n";
}

template<typename T>
void debugarr(T arr, int n) {
    for (int i=0;i<n;i++) cout << arr[i] << " ";
    cout << "\n";
}

ll __min(ll a, ll b) {
    return a < b ? a : b;
}

ll __max(ll a, ll b) {
    return a > b ? a : b;
}

const int maxn = 2e3+100;
ll n, m, t, k, q;
unordered_map<ll,ll> u;

ll l[maxn], r[maxn], w[maxn];
ll dp[maxn];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin>>n>>k;
    for (int i=0;i<n;i++) cin>>l[i]>>r[i]>>w[i];
    for (int i=0;i<n;i++) dp[i]=INT_MAX;
    dp[0]=0;
    ll ans=INT_MAX;
    for (int i=0;i<n;i++) {
        if (dp[i]<INT_MAX) {
            ll st=k;
            ll tot=dp[i];
            bool pos=true;
            for (int j=i;j<n;j++) {
                ll steps=(w[j]-st+k-1)/k;
                if (l[j]+steps>r[j]) {
                    pos=false;
                    break;
                }
                tot += w[j];
                st = (st-w[j])%k;
                st = (st + k)%k;

                if (j+1<n && l[i]+steps<l[j+1])
                    dp[j+1]=__min(dp[j+1],tot+st);
            }
            if (pos)
                ans=__min(ans,tot);
        }
    }
    if (ans>=INT_MAX) ans=-1;
    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define mod 1000000007
#define mod2 998244353
#define pi pair<ll,ll>
#define pii pair<ll,pair<ll,ll>>
#define vi vector<ll>
#define pb push_back
#define ld long double
#define vpi vector<pair<ll,ll>>
#define vvi vector<vector<ll>>

#define debug(a) { cout << #a << " = " << a << endl;}

void sep() {
    cout << "-------------BLOCK-----------------\n";
}

template<typename T>
void debugv(vector<T> arr) {
    for (auto t: arr)
        cout << t << " ";
    cout << "\n";
}

template<typename T>
void debugarr(T arr, int n) {
    for (int i=0;i<n;i++) cout << arr[i] << " ";
    cout << "\n";
}

ll __min(ll a, ll b) {
    return a < b ? a : b;
}

ll __max(ll a, ll b) {
    return a > b ? a : b;
}

const int maxn = 2e3+100;
ll n, m, t, k, q;
unordered_map<ll,ll> u;
ll l[maxn],r[maxn],w[maxn];
vvi dp;

ll go(ll curr, ll last, ll prev) {
    if (curr == n) {
        return -prev;
    }
    if (dp[curr][last]<INT_MAX) return dp[curr][last];
    ll used = w[curr] - prev;
    ll reloads = (used + k - 1)/k;
    if (r[curr]-l[curr]<reloads) return dp[curr][last]=INT_MAX;
    used=reloads*k;
    ll left = used + prev - w[curr];
    ll ans = used + go(curr+1,last, left);
    if (curr+1<n&&l[curr]+reloads<l[curr+1])
        ans=__min(ans,used+k+go(curr+1,curr+1,k));
    return dp[curr][last]=ans;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin>>n>>k;
    for (int i=0;i<n;i++) cin>>l[i]>>r[i]>>w[i];
    dp.resize(n+1,vi(n+1,INT_MAX));
    ll ans = k + go(0,0,k);
    if (ans >= INT_MAX) cout << -1 << endl;
    else cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define mod 1000000007
#define mod2 998244353
#define pi pair<ll,ll>
#define pii pair<ll,pair<ll,ll>>
#define vi vector<ll>
#define pb push_back
#define ld long double
#define vpi vector<pair<ll,ll>>
#define vvi vector<vector<ll>>

#define debug(a) { cout << #a << " = " << a << endl;}

void sep() {
    cout << "-------------BLOCK-----------------\n";
}

template<typename T>
void debugv(vector<T> arr) {
    for (auto t: arr)
        cout << t << " ";
    cout << "\n";
}

template<typename T>
void debugarr(T arr, int n) {
    for (int i=0;i<n;i++) cout << arr[i] << " ";
    cout << "\n";
}

ll __min(ll a, ll b) {
    return a < b ? a : b;
}

ll __max(ll a, ll b) {
    return a > b ? a : b;
}

const int maxn = 2e3+100;
ll n, m, t, k, q;
unordered_map<ll,ll> u;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin>>n>>k;
    ll l[n], r[n], w[n];
    ll dp[n];
    for (int i=0;i<n;i++) cin>>l[i]>>r[i]>>w[i];
    for (int i=0;i<n;i++) dp[i]=INT_MAX;
    dp[0]=0;
    ll ans=INT_MAX;
    for (ll i=0;i<n;i++) {
        if (dp[i]<INT_MAX) {
            ll run=k;
            ll tot=dp[i];
            bool pos=true;
            for (ll j=i;j<n;j++) {
                ll runeps=(w[j]-run+k-1)/k;
                if (l[j]+runeps>r[j]) {
                    pos=false;
                    break;
                }
                tot += w[j];
                run = (run-w[j])%k;
                run = (run + k)%k;

                if (j+1<n && l[j]+runeps<l[j+1])
                    dp[j+1]=__min(dp[j+1],tot+run);
            }
            if (pos)
                ans=__min(ans,tot);
        }
    }
    if (ans>=INT_MAX) ans=-1;
    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define mod 1000000007
#define mod2 998244353
#define pi pair<ll,ll>
#define pii pair<ll,pair<ll,ll>>
#define vi vector<ll>
#define pb push_back
#define ld long double
#define vpi vector<pair<ll,ll>>
#define vvi vector<vector<ll>>

#define debug(a) { cout << #a << " = " << a << endl;}

void sep() {
    cout << "-------------BLOCK-----------------\n";
}

template<typename T>
void debugv(vector<T> arr) {
    for (auto t: arr)
        cout << t << " ";
    cout << "\n";
}

template<typename T>
void debugarr(T arr, int n) {
    for (int i=0;i<n;i++) cout << arr[i] << " ";
    cout << "\n";
}

ll __min(ll a, ll b) {
    return a < b ? a : b;
}

ll __max(ll a, ll b) {
    return a > b ? a : b;
}

const int maxn = 2e3+100;
ll n, m, t, k, q;
unordered_map<ll,ll> u;

ll l[maxn], r[maxn], w[maxn];
ll dp[maxn];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin>>n>>k;
    for (int i=0;i<n;i++) cin>>l[i]>>r[i]>>w[i];
    for (int i=0;i<n;i++) dp[i]=INT_MAX;
    dp[0]=0;
    ll ans=INT_MAX;
    for (int i=0;i<n;i++) {
        if (dp[i]<INT_MAX) {
            ll run=k;
            ll tot=dp[i];
            bool pos=true;
            for (int j=i;j<n;j++) {
                ll runeps=(w[j]-run+k-1)/k;
                if (l[j]+runeps>r[j]) {
                    pos=false;
                    break;
                }
                tot += w[j];
                run = (run-w[j])%k;
                run = (run + k)%k;

                if (j+1<n && l[j]+runeps<l[j+1])
                    dp[j+1]=__min(dp[j+1],tot+run);
            }
            if (pos)
                ans=__min(ans,tot);
        }
    }
    if (ans>=INT_MAX) ans=-1;
    cout << ans << endl;
}


#include"bits/stdc++.h"
using namespace std;
#define ri register int
#define ll long long
#define N 2010
ll n,k,l[N],r[N],a[N],f[N];
int main(){
	cin>>n>>k;
	for(ri i=1;i<=n;++i) cin>>l[i]>>r[i]>>a[i];
	for(ri i=n;i>=1;--i){
		ll need=a[i];if(r[i]==l[i+1]) need+=f[i+1];
		if(need>(r[i]-l[i]+1)*k){
			cout<<-1<<endl;
			return 0;
		}
		f[i]=max(0ll,need-(r[i]-l[i])*k);
	}
	ll cur=k,ans=0;
	for(ri i=1;i<=n;++i){
		if(cur<f[i]){
			ans+=cur;
			cur=k;
		}
		ans+=a[i];
		cur=((cur-a[i])%k+k)%k;
	}
	cout<<ans;
	return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#define maxn 2010
#define ll long long
#define pb push_back
#define INF 10000000000000000ll
using namespace std;

int n; ll m;

struct node {
    int l, r, v;
} a[maxn];

ll f[maxn];
int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) cin >> a[i].l >> a[i].r >> a[i].v;
    for (int i = n; i; --i) {
        if (a[i].r == a[i + 1].l) f[i] = max(0ll, a[i].v + f[i + 1] - (a[i].r - a[i].l) * m);
        else f[i] = max(0ll, a[i].v - (a[i].r - a[i].l) * m);
        if (f[i] > m) return puts("-1"), 0;
    }
    ll ans = 0, tp = m;
    for (int i = 1; i <= n; ++i) {
        if (tp < f[i]) ans += tp, tp = m;
        ans += a[i].v;
        if (a[i].v >= tp) tp = m - (a[i].v - tp) % m;
        else tp -= a[i].v;
    } cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define lc (x << 1)
#define rc (x << 1 | 1)
#define gc getchar() //(p1==p2&&(p2=(p1=buf)+fread(buf,1,size,stdin),p1==p2)?EOF:*p1++)
#define mk make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define IT iterator
#define V vector
#define TP template <class o>
#define TPP template <typename t1, typename t2>
#define SZ(a) ((int)a.size())
#define all(a) a.begin(), a.end()
#define rep(i, a, b) for (auto i = a; i <= b; i++)
#define REP(i, a, b) for (auto i = b; i >= a; i--)
#define debug(x) cerr << #x << " = " << x << endl
using namespace std;
typedef unsigned ui;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
const int N = 2010, size = 1 << 20, mod = 998244353, inf = 2e9;
const ll INF = 1e15;

// char buf[size],*p1=buf,*p2=buf;
TP void qr(o& x) {
    char c = gc;
    x = 0;
    int f = 1;
    while (!isdigit(c)) {
        if (c == '-')
            f = -1;
        c = gc;
    }
    while (isdigit(c))
        x = x * 10 + c - '0', c = gc;
    x *= f;
}
TP void qw(o x) {
    if (x / 10)
        qw(x / 10);
    putchar(x % 10 + '0');
}
TP void pr1(o x) {
    if (x < 0)
        x = -x, putchar('-');
    qw(x);
    putchar(' ');
}
TP void pr2(o x) {
    if (x < 0)
        x = -x, putchar('-');
    qw(x);
    putchar('\n');
}

// math
ll gcd(ll a, ll b) { return !a ? b : gcd(b % a, a); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll power(ll a, ll b = mod - 2, ll p = mod) {
    ll c = 1;
    while (b) {
        if (b & 1)
            c = c * a % p;
        b /= 2;
        a = a * a % p;
    }
    return c;
}

TP void cmax(o& x, o y) {
    if (x < y)
        x = y;
}
void cmax(int& x, int y) {
    x = x - y >> 31 ? y : x;
    ;
}
TP void cmin(o& x, o y) {
    if (x > y)
        x = y;
}
void cmin(int& x, int y) { x = x - y >> 31 ? x : y; }
TPP void ad(t1& x, t2 y) {
    x += y;
    if (x >= mod)
        x -= mod;
}
TPP void dl(t1& x, t2 y) {
    x -= y;
    if (x < 0)
        x += mod;
}

// dbinom
ll jc[N], inv[N];
void jc_init(int n) {
    jc[0] = 1;
    for (int i = 1; i <= n; i++)
        jc[i] = jc[i - 1] * i % mod;
    inv[n] = power(jc[n]);
    for (int i = n; i; i--)
        inv[i - 1] = inv[i] * i % mod;
}
ll C(int x, int y) {
    if (x < y || y < 0)
        return 0;
    return jc[x] * inv[y] % mod * inv[x - y] % mod;
}

int n, l[N],r[N],a[N],f[N],k;

void solve() {
	qr(n); qr(k);
	rep(i,1,n) qr(l[i]),qr(r[i]),qr(a[i]);
	REP(i,1,n) {
		int need=a[i];
		if(r[i]==l[i+1]) need += f[i+1];
		if((r-l+1)*k<need) puts("-1"),exit(0);
		f[i]=max(0LL,need-1LL*(r-l)*k);
	}
	int cur=k; ll ans=0;
	rep(i,1,n) {
		if(cur<f[i]) {
			ans += cur;
			cur = k;
		}
		ans += a[i];
		cur=((cur-a[i])%k+k)%k;
	}
	pr2(ans);
}

int main() {
    int T = 1;
//    qr(T);
    while (T--)
        solve();
    return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define lc (x << 1)
#define rc (x << 1 | 1)
#define gc getchar() //(p1==p2&&(p2=(p1=buf)+fread(buf,1,size,stdin),p1==p2)?EOF:*p1++)
#define mk make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define IT iterator
#define V vector
#define TP template <class o>
#define TPP template <typename t1, typename t2>
#define SZ(a) ((int)a.size())
#define all(a) a.begin(), a.end()
#define rep(i, a, b) for (auto i = a; i <= b; i++)
#define REP(i, a, b) for (auto i = b; i >= a; i--)
#define debug(x) cerr << #x << " = " << x << endl
using namespace std;
typedef unsigned ui;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
const int N = 2010, size = 1 << 20, mod = 998244353, inf = 2e9;
const ll INF = 1e15;

// char buf[size],*p1=buf,*p2=buf;
TP void qr(o& x) {
    char c = gc;
    x = 0;
    int f = 1;
    while (!isdigit(c)) {
        if (c == '-')
            f = -1;
        c = gc;
    }
    while (isdigit(c))
        x = x * 10 + c - '0', c = gc;
    x *= f;
}
TP void qw(o x) {
    if (x / 10)
        qw(x / 10);
    putchar(x % 10 + '0');
}
TP void pr1(o x) {
    if (x < 0)
        x = -x, putchar('-');
    qw(x);
    putchar(' ');
}
TP void pr2(o x) {
    if (x < 0)
        x = -x, putchar('-');
    qw(x);
    putchar('\n');
}

// math
ll gcd(ll a, ll b) { return !a ? b : gcd(b % a, a); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll power(ll a, ll b = mod - 2, ll p = mod) {
    ll c = 1;
    while (b) {
        if (b & 1)
            c = c * a % p;
        b /= 2;
        a = a * a % p;
    }
    return c;
}

TP void cmax(o& x, o y) {
    if (x < y)
        x = y;
}
void cmax(int& x, int y) {
    x = x - y >> 31 ? y : x;
    ;
}
TP void cmin(o& x, o y) {
    if (x > y)
        x = y;
}
void cmin(int& x, int y) { x = x - y >> 31 ? x : y; }
TPP void ad(t1& x, t2 y) {
    x += y;
    if (x >= mod)
        x -= mod;
}
TPP void dl(t1& x, t2 y) {
    x -= y;
    if (x < 0)
        x += mod;
}

// dbinom
ll jc[N], inv[N];
void jc_init(int n) {
    jc[0] = 1;
    for (int i = 1; i <= n; i++)
        jc[i] = jc[i - 1] * i % mod;
    inv[n] = power(jc[n]);
    for (int i = n; i; i--)
        inv[i - 1] = inv[i] * i % mod;
}
ll C(int x, int y) {
    if (x < y || y < 0)
        return 0;
    return jc[x] * inv[y] % mod * inv[x - y] % mod;
}

int n, l[N],r[N],a[N],f[N],k;

void solve() {
	qr(n); qr(k);
	rep(i,1,n) qr(l[i]),qr(r[i]),qr(a[i]);
	REP(i,1,n) {
		int need=a[i];
		if(r[i]==l[i+1]) need += f[i+1];
		if(1LL*(r[i]-l[i]+1)*k<need) puts("-1"),exit(0);
		f[i]=max(0LL,need-1LL*(r-l)*k);
	}
	int cur=k; ll ans=0;
	rep(i,1,n) {
		if(cur<f[i]) {
			ans += cur;
			cur = k;
		}
		ans += a[i];
		cur=((cur-a[i])%k+k)%k;
	}
	pr2(ans);
}

int main() {
    int T = 1;
//    qr(T);
    while (T--)
        solve();
    return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define lc (x << 1)
#define rc (x << 1 | 1)
#define gc getchar() //(p1==p2&&(p2=(p1=buf)+fread(buf,1,size,stdin),p1==p2)?EOF:*p1++)
#define mk make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define IT iterator
#define V vector
#define TP template <class o>
#define TPP template <typename t1, typename t2>
#define SZ(a) ((int)a.size())
#define all(a) a.begin(), a.end()
#define rep(i, a, b) for (auto i = a; i <= b; i++)
#define REP(i, a, b) for (auto i = b; i >= a; i--)
#define debug(x) cerr << #x << " = " << x << endl
using namespace std;
typedef unsigned ui;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
const int N = 2010, size = 1 << 20, mod = 998244353, inf = 2e9;
const ll INF = 1e15;

// char buf[size],*p1=buf,*p2=buf;
TP void qr(o& x) {
    char c = gc;
    x = 0;
    int f = 1;
    while (!isdigit(c)) {
        if (c == '-')
            f = -1;
        c = gc;
    }
    while (isdigit(c))
        x = x * 10 + c - '0', c = gc;
    x *= f;
}
TP void qw(o x) {
    if (x / 10)
        qw(x / 10);
    putchar(x % 10 + '0');
}
TP void pr1(o x) {
    if (x < 0)
        x = -x, putchar('-');
    qw(x);
    putchar(' ');
}
TP void pr2(o x) {
    if (x < 0)
        x = -x, putchar('-');
    qw(x);
    putchar('\n');
}

// math
ll gcd(ll a, ll b) { return !a ? b : gcd(b % a, a); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll power(ll a, ll b = mod - 2, ll p = mod) {
    ll c = 1;
    while (b) {
        if (b & 1)
            c = c * a % p;
        b /= 2;
        a = a * a % p;
    }
    return c;
}

TP void cmax(o& x, o y) {
    if (x < y)
        x = y;
}
void cmax(int& x, int y) {
    x = x - y >> 31 ? y : x;
    ;
}
TP void cmin(o& x, o y) {
    if (x > y)
        x = y;
}
void cmin(int& x, int y) { x = x - y >> 31 ? x : y; }
TPP void ad(t1& x, t2 y) {
    x += y;
    if (x >= mod)
        x -= mod;
}
TPP void dl(t1& x, t2 y) {
    x -= y;
    if (x < 0)
        x += mod;
}

// dbinom
ll jc[N], inv[N];
void jc_init(int n) {
    jc[0] = 1;
    for (int i = 1; i <= n; i++)
        jc[i] = jc[i - 1] * i % mod;
    inv[n] = power(jc[n]);
    for (int i = n; i; i--)
        inv[i - 1] = inv[i] * i % mod;
}
ll C(int x, int y) {
    if (x < y || y < 0)
        return 0;
    return jc[x] * inv[y] % mod * inv[x - y] % mod;
}

int n, l[N],r[N],a[N],f[N],k;

void solve() {
	qr(n); qr(k);
	rep(i,1,n) qr(l[i]),qr(r[i]),qr(a[i]);
	REP(i,1,n) {
		int need=a[i];
		if(r[i]==l[i+1]) need += f[i+1];
		if(1LL*(r[i]-l[i]+1)*k<need) puts("-1"),exit(0);
		f[i]=max(0LL,need-1LL*(r[i]-l[i])*k);
	}
	int cur=k; ll ans=0;
	rep(i,1,n) {
		if(cur<f[i]) {
			ans += cur;
			cur = k;
		}
		ans += a[i];
		cur=((cur-a[i])%k+k)%k;
	}
	pr2(ans);
}

int main() {
    int T = 1;
//    qr(T);
    while (T--)
        solve();
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>
#include <set>
#include <queue>
#include <cstring>
#define LL long long
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;

const int maxn = 2e3+10;

int n, l[maxn], r[maxn], a[maxn];
LL dp[maxn], ans = INF, k;

int main() {
	scanf("%d%lld", &n, &k);
	for (int i = 1; i <= n; i++) {
		scanf("%d%d%d", l + i, r + i, a + i);
		dp[i] = INF;
	}
	dp[0] = 0;
	for (int i = 0; i < n; i++) {
		LL remain = k, bullet = 0;
		for (int j = i + 1; j <= n; j++) {
			LL reload = (max(0ll, a[j] - remain) + k - 1) / k;
			// can't clear this wave
			if (reload > r[j] - l[j])
				break;
			else {
				remain = remain + reload * k - a[j];
				bullet += a[j];
				//there's time between this wave and next wave
				//or there's time in this wave
				if (j < n) {
					if (reload < l[j+1] - l[j])
						dp[j] = min(dp[j], dp[i] + bullet + remain);
				}
				else
					ans = min(ans, dp[i] + bullet);
			}
		}
	}
	printf("%lld\n", ans < INF ? ans : -1);
	return 0;
}