#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N = 2000 + 10;
int n, k, l[N], r[N], a[N];
LL dp[N];  // 表示第i波怪物来之前至少要准备dp[i]的子弹

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--) {
        LL need = a[i];
        if (r[i] == l[i + 1]) {
            need += dp[i + 1];
        }
        if (1ll * (r[i] - l[i] + 1) * k < need) {
            return cout << -1, 0;
        }
        dp[i] = max(0ll, need - 1ll * (r[i] - l[i]) * k);
    }
    int res = k;
    LL ans = 0;
    for (int i = 1; i <= n; i++) {
        if (res < dp[i]) {
            ans += res;
            res = k;
        }
        ans += a[i];
        cout << ans << endl;
        res = ((res - a[i]) % k + k) % k;
    }
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N = 2000 + 10;
int n, k, l[N], r[N], a[N];
LL dp[N];  // 表示第i波怪物来之前至少还需要准备dp[i]的子弹

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--) {
        LL need = a[i];
        if (r[i] == l[i + 1]) {
            need += dp[i + 1];
        }
        if (1ll * (r[i] - l[i] + 1) * k < need) {
            return cout << -1, 0;
        }
        dp[i] = max(0ll, need - 1ll * (r[i] - l[i]) * k);
    }
    int res = k; //初始有K发子弹
    LL ans = 0;
    for (int i = 1; i <= n; i++) {
        if (res < dp[i]) { //如果当前子弹数量不够那么肯定要把当前剩余子弹都扔了，然后装弹
            ans += res;
            res = k;
        }
        ans += a[i]; //打出ai发子弹消灭第i波怪兽
        res = ((res - a[i]) % k + k) % k; //消灭第i波怪兽后枪内剩余的子弹数量
    }
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
int n, k, l[N], r[N], a[N];
LL dp[N];

int main() {
    ios::sync_with_stdio(false);
    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 (r[i] == l[i + 1]) need += dp[i + 1];
        if (1ll * (r[i] - l[i] + 1) * k < need) return cout << -1, 0;
        dp[i] = max(0ll, need - 1ll * (r[i] - l[i]) * k);
    }
    int res = k;
    LL ans = 0;
    for (int i = 1; i <= n; i++) {
        if (res < dp[i]) {
            ans += res;
            res = k;
        }
        ans += a[i];
        res = ((res - a[i]) % k + k) % k;
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N = 200000 + 10;
int n, k, l[N], r[N], a[N];
LL dp[N];  // 表示第i波怪物来之前至少要准备dp[i]的子弹

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--) {
        LL need = a[i];
        if (r[i] == l[i + 1]) {
            need += dp[i + 1];
        }
        if (1ll * (r[i] - l[i] + 1) * k < need) {
            return cout << -1, 0;
        }
        dp[i] = max(0ll, need - 1ll * (r[i] - l[i]) * k);
    }
    int res = k;
    LL ans = 0;
    for (int i = 1; i <= n; i++) {
        if (res < dp[i]) {
            ans += res;
            res = k;
        }
        ans += a[i];
        res = ((res - a[i]) % k + k) % k;
    }
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N = 200000 + 10;
int n, k, l[N], r[N], a[N];
LL dp[N];  // 表示第i波怪物来之前至少要准备dp[i]的子弹

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--) {
        LL need = a[i];
        if (r[i] == l[i + 1]) {
            need += dp[i + 1];
        }
        if (1ll * (r[i] - l[i] + 1) * k < need) {
            return cout << -1, 0;
        }
        dp[i] = max(0ll, need - 1ll * (r[i] - l[i]) * k);
    }
    int res = k;
    LL ans = 0;
    for (int i = 1; i <= n; i++) {
        if (res < dp[i]) {
            ans += res;
            res = k;
        }
        ans += a[i];
        cout << ans << endl;
        res = ((res - a[i]) % k + k) % k;
    }
    cout << ans;
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <iostream>
#include <cassert>
#include <cmath>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <cstdlib>

using namespace std;

#define INF 1e+9
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define li long long
#define pii pair<int, int>
#define vi vector<int>
#define li long long

#define forn(i, n) for (int i = 0; i < (int)n; i++)
#define fore(i, b, e) for (int i = (int)b; i <= (int)e; i++)

const int MAXN = 2005;

int l[MAXN];
int r[MAXN];
int monsters[MAXN];

li dp[MAXN][MAXN];

void upd(li& x, li y) {
	x = min(x, y);
}

int main() {
	int n, k;
	scanf("%d%d", &n, &k);
	forn(i, n) {
		scanf("%d%d%d", &l[i], &r[i], &monsters[i]);
	}
	fore(i, 0, n)
		fore(j, 0, n)
			dp[i][j] = INF;
	dp[0][0] = 0;
	fore(last_reload, 0, n - 1) {
		int rem = k;
		fore(i, last_reload, n - 2) {
			if (dp[i][last_reload] == INF)
				continue;
			// reload now
			int cur_monsters = max(0, monsters[i] - rem);
			if (cur_monsters <= (li)(r[i] - l[i] - 1) * k) {
				int next_monsters = max(0, monsters[i + 1] - k);
				if (next_monsters <= (li)(r[i + 1] - l[i + 1]) * k) {
					int wasted = k - cur_monsters % k;
					if (wasted == k)
						wasted = 0;
					upd(dp[i + 1][i + 1], dp[i][last_reload] + wasted);
				}
			}
			// do not reload
			if (monsters[i + 1] <= (li)(r[i + 1] - l[i + 1]) * k) {
				upd(dp[i + 1][last_reload], dp[i][last_reload]);
			}
			rem = (monsters[i + 1] % k - rem + k) % k;
		}
	}
	li ans = INF;
	fore(last_reload, 0, n - 1)
		ans = min(ans, dp[n - 1][last_reload]);
	if (ans == INF) {
		cout << -1;
	} else {
		forn(i, n)
			ans += monsters[i];
		cout << ans;
	}
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <iostream>
#include <cassert>
#include <cmath>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <cstdlib>

using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define li long long
#define pii pair<int, int>
#define vi vector<int>
#define li long long

#define forn(i, n) for (int i = 0; i < (int)n; i++)
#define fore(i, b, e) for (int i = (int)b; i <= (int)e; i++)

const int MAXN = 2005;
const li INF = 1e18;

int l[MAXN];
int r[MAXN];
int monsters[MAXN];

li dp[MAXN][MAXN];

void upd(li& x, li y) {
	x = min(x, y);
}

int main() {
	int n, k;
	scanf("%d%d", &n, &k);
	li sum = 0;
	forn(i, n) {
		scanf("%d%d%d", &l[i], &r[i], &monsters[i]);
		sum += monsters[i];
	}
	//cerr << "sum = " << sum << endl;
	fore(i, 0, n)
		fore(j, 0, n)
			dp[i][j] = INF;
	dp[0][0] = 0;
	fore(last_reload, 0, n - 1) {
		int rem = k;
		fore(i, last_reload, n - 1) {
			//if (dp[i][last_reload] != INF)
			//	printf("dp[%d][%d] = %lld\n", i, last_reload, dp[i][last_reload]);
			if (dp[i][last_reload] == INF || i == n - 1)
				continue;
			int next_rem = (rem - monsters[i] % k + k) % k;
			// reload now
			int cur_monsters = max(0, monsters[i] - rem);
			int delta = r[i] == l[i + 1] ? -1 : 0;
			if (cur_monsters <= (li)(r[i] - l[i] + delta) * k) {
				int next_monsters = max(0, monsters[i + 1] - k);
				if (next_monsters <= (li)(r[i + 1] - l[i + 1]) * k) {
					upd(dp[i + 1][i + 1], dp[i][last_reload] + next_rem);
				}
			}
			// do not reload
			rem = next_rem;
			int next_monsters = max(0, monsters[i + 1] - rem);
			if (next_monsters <= (li)(r[i + 1] - l[i + 1]) * k) {
				upd(dp[i + 1][last_reload], dp[i][last_reload]);
			}
		}
	}
	li ans = INF;
	fore(last_reload, 0, n - 1)
		ans = min(ans, dp[n - 1][last_reload]);
	if (ans == INF) {
		cout << -1;
	} else {
		cout << ans + sum;
	}
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <iostream>
#include <cassert>
#include <cmath>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <cstdlib>

using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define li long long
#define pii pair<int, int>
#define vi vector<int>
#define li long long

#define forn(i, n) for (int i = 0; i < (int)n; i++)
#define fore(i, b, e) for (int i = (int)b; i <= (int)e; i++)

const int MAXN = 2005;
const li INF = 1e18;

int l[MAXN];
int r[MAXN];
int monsters[MAXN];

li dp[MAXN][MAXN];

void upd(li& x, li y) {
	x = min(x, y);
}

int main() {
	int n, k;
	scanf("%d%d", &n, &k);
	li sum = 0;
	forn(i, n) {
		scanf("%d%d%d", &l[i], &r[i], &monsters[i]);
		sum += monsters[i];
	}
	cerr << "sum = " << sum << endl;
	fore(i, 0, n)
		fore(j, 0, n)
			dp[i][j] = INF;
	dp[0][0] = 0;
	fore(last_reload, 0, n - 1) {
		int rem = k;
		fore(i, last_reload, n - 1) {
			//if (dp[i][last_reload] != INF)
			//	printf("dp[%d][%d] = %lld\n", i, last_reload, dp[i][last_reload]);
			if (dp[i][last_reload] == INF || i == n - 1)
				continue;
			int next_rem = (rem - monsters[i] % k + k) % k;
			// reload now
			int cur_monsters = max(0, monsters[i] - rem);
			int delta = r[i] == l[i + 1] ? -1 : 0;
			if (cur_monsters <= (li)(r[i] - l[i] + delta) * k) {
				int next_monsters = max(0, monsters[i + 1] - k);
				if (next_monsters <= (li)(r[i + 1] - l[i + 1]) * k) {
					upd(dp[i + 1][i + 1], dp[i][last_reload] + next_rem);
				}
			}
			// do not reload
			rem = next_rem;
			if (cur_monsters <= (li)(r[i] - l[i]) * k) {
				int next_monsters = max(0, monsters[i + 1] - rem);
				if (next_monsters <= (li)(r[i + 1] - l[i + 1]) * k) {
					upd(dp[i + 1][last_reload], dp[i][last_reload]);
				}
			}
		}
	}
	li ans = INF;
	fore(last_reload, 0, n - 1)
		ans = min(ans, dp[n - 1][last_reload]);
	if (ans == INF) {
		cout << -1;
	} else {
		cout << ans + sum;
	}
}


#include "bits/stdc++.h"
//#include<ext/pb_ds/assoc_container.hpp>
//#include <random>

using namespace std;
//using namespace __gnu_pbds;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    os << '{';
    string sep;
    for (const auto& x : v) os << sep << x, sep = ", ";
    return os << '}';
}

template <typename T, size_t size>
ostream& operator<<(ostream& os, const array<T, size>& arr) {
    os << '{';
    string sep;
    for (const auto& x : arr) os << sep << x, sep = ", ";
    return os << '}';
}

template <typename A, typename B>
ostream& operator<<(ostream& os, const pair<A, B>& p) { return os << '(' << p.first << ", " << p.second << ')'; }

#define all(x) x.begin(), x.end()
#define bits(x) __builtin_popcount(x)
#define int ll
#define sz(x) int((x).size())

typedef long long ll;
typedef long double ld;
typedef __float128 lld;
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> dd;

/*void random() {
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
}*/

template <class FR>
inline FR max(FR& a, const FR& b) {
    return (a > b) ? a : b;
}

template <class FR>
inline FR min(FR& a, const FR& b) {
    return (a > b) ? b : a;
}

const int MAXN = (int) 1e6 + 10;
const int MOD = (int) 1e9 + 7;
const int INF = 1e13 + 1;

struct wave {
    int l, r, a;
};

int32_t main() {
//    freopen("firesafe.in", "r", stdin), freopen("firesafe.out", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, k;
    cin >> n >> k;
    vector<wave> waves(n);
    for (int i = 0; i < n; ++i) {
        int l, r, a;
        cin >> l >> r >> a;
        waves[i] = {l, r, a};
    }
    vector<int> dp(n, INF);
    dp[0] = 0;
    int ans = INF;
    for (int i = 0; i < n; ++i) {
        int bullets = k;
        int used = dp[i];
        if (used == INF) {
            continue;
        }
        for (int j = i; j < n; ++j) {
            auto wv = waves[j];
            if (((wv.a - bullets) - (wv.r - wv.l) * k) > 0) {
                break;
            }
            int ost = 0;
            int reload = 0;
            used += wv.a;
            if (wv.a <= bullets) {
                ost = bullets - wv.a;
            } else {
                reload = (wv.a - bullets + k - 1) / k;
                ost = k - (wv.a - bullets) + (reload - 1) * k;
            }
//            cout << bullets << ' ' << wv.a << ' ' << i << ' ' << j << ' ' << ost << ' ' << reload << '\n';
            if (j + 1 < n) {
                if (waves[j].l + reload < waves[j + 1].l)
                    dp[j + 1] = min(dp[j + 1], used + ost);
            } else {
                ans = min(ans, used);
            }
            bullets = ost;
        }
    }
    if (ans == INF) {
        return cout << -1,0;
    }
    cout << ans << '\n';
    return 0;
}


#include "bits/stdc++.h"
//#include<ext/pb_ds/assoc_container.hpp>
//#include <random>

using namespace std;
//using namespace __gnu_pbds;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    os << '{';
    string sep;
    for (const auto& x : v) os << sep << x, sep = ", ";
    return os << '}';
}

template <typename T, size_t size>
ostream& operator<<(ostream& os, const array<T, size>& arr) {
    os << '{';
    string sep;
    for (const auto& x : arr) os << sep << x, sep = ", ";
    return os << '}';
}

template <typename A, typename B>
ostream& operator<<(ostream& os, const pair<A, B>& p) { return os << '(' << p.first << ", " << p.second << ')'; }

#define all(x) x.begin(), x.end()
#define bits(x) __builtin_popcount(x)
#define int ll
#define sz(x) int((x).size())

typedef long long ll;
typedef long double ld;
typedef __float128 lld;
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> dd;

/*void random() {
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
}*/

template <class FR>
inline FR max(FR& a, const FR& b) {
    return (a > b) ? a : b;
}

template <class FR>
inline FR min(FR& a, const FR& b) {
    return (a > b) ? b : a;
}

const int MAXN = (int) 1e6 + 10;
const int MOD = (int) 1e9 + 7;
const int INF = 1e9 + 1;

struct wave {
    int l, r, a;
};

int32_t main() {
//    freopen("firesafe.in", "r", stdin), freopen("firesafe.out", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, k;
    cin >> n >> k;
    vector<wave> waves(n);
    for (int i = 0; i < n; ++i) {
        int l, r, a;
        cin >> l >> r >> a;
        waves[i] = {l, r, a};
    }
    vector<int> dp(n, INF);
    dp[0] = 0;
    int ans = INF;
    for (int i = 0; i < n; ++i) {
        int bullets = k;
        int used = dp[i];
        for (int j = i; j < n; ++j) {
            auto wv = waves[j];
            if (((wv.a - bullets) - (wv.r - wv.l) * k) > 0) {
                break;
            }
            int ost = 0;
            int reload = 0;
            used += wv.a;
            if (wv.a <= bullets) {
                ost = bullets - wv.a;
            } else {
                reload = (wv.a - bullets + k - 1) / k;
                ost = k - (wv.a - bullets) + (reload - 1) * k;
            }
//            cout << bullets << ' ' << wv.a << ' ' << i << ' ' << j << ' ' << ost << ' ' << reload << '\n';
            if (j + 1 < n) {
                if (waves[j].r + 1 < waves[j + 1].l)
                    dp[j + 1] = min(dp[j + 1], used + ost);
            } else {
                ans = min(ans, used);
            }
            bullets = ost;
        }
    }
    if (ans == INF) {
        return cout << -1,0;
    }
    cout << ans << '\n';
    return 0;
}


#include "bits/stdc++.h"
//#include<ext/pb_ds/assoc_container.hpp>
//#include <random>

using namespace std;
//using namespace __gnu_pbds;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    os << '{';
    string sep;
    for (const auto& x : v) os << sep << x, sep = ", ";
    return os << '}';
}

template <typename T, size_t size>
ostream& operator<<(ostream& os, const array<T, size>& arr) {
    os << '{';
    string sep;
    for (const auto& x : arr) os << sep << x, sep = ", ";
    return os << '}';
}

template <typename A, typename B>
ostream& operator<<(ostream& os, const pair<A, B>& p) { return os << '(' << p.first << ", " << p.second << ')'; }

#define all(x) x.begin(), x.end()
#define bits(x) __builtin_popcount(x)
#define int ll
#define sz(x) int((x).size())

typedef long long ll;
typedef long double ld;
typedef __float128 lld;
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> dd;

/*void random() {
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
}*/

template <class FR>
inline FR max(FR& a, const FR& b) {
    return (a > b) ? a : b;
}

template <class FR>
inline FR min(FR& a, const FR& b) {
    return (a > b) ? b : a;
}

const int MAXN = (int) 1e6 + 10;
const int MOD = (int) 1e9 + 7;
const int INF = 1e9 + 1;

struct wave {
    int l, r, a;
};

int32_t main() {
//    freopen("firesafe.in", "r", stdin), freopen("firesafe.out", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, k;
    cin >> n >> k;
    vector<wave> waves(n);
    for (int i = 0; i < n; ++i) {
        int l, r, a;
        cin >> l >> r >> a;
        waves[i] = {l, r, a};
    }
    vector<int> dp(n, INF);
    dp[0] = 0;
    int ans = INF;
    for (int i = 0; i < n; ++i) {
        int bullets = k;
        int used = dp[i];
        if (used == INF) {
            continue;
        }
        for (int j = i; j < n; ++j) {
            auto wv = waves[j];
            if (((wv.a - bullets) - (wv.r - wv.l) * k) > 0) {
                break;
            }
            int ost = 0;
            int reload = 0;
            used += wv.a;
            if (wv.a <= bullets) {
                ost = bullets - wv.a;
            } else {
                reload = (wv.a - bullets + k - 1) / k;
                ost = k - (wv.a - bullets) + (reload - 1) * k;
            }
//            cout << bullets << ' ' << wv.a << ' ' << i << ' ' << j << ' ' << ost << ' ' << reload << '\n';
            if (j + 1 < n) {
                if (waves[j].l + reload < waves[j + 1].l)
                    dp[j + 1] = min(dp[j + 1], used + ost);
            } else {
                ans = min(ans, used);
            }
            bullets = ost;
        }
    }
    if (ans == INF) {
        return cout << -1,0;
    }
    cout << ans << '\n';
    return 0;
}


#include "bits/stdc++.h"
//#include<ext/pb_ds/assoc_container.hpp>
//#include <random>

using namespace std;
//using namespace __gnu_pbds;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    os << '{';
    string sep;
    for (const auto& x : v) os << sep << x, sep = ", ";
    return os << '}';
}

template <typename T, size_t size>
ostream& operator<<(ostream& os, const array<T, size>& arr) {
    os << '{';
    string sep;
    for (const auto& x : arr) os << sep << x, sep = ", ";
    return os << '}';
}

template <typename A, typename B>
ostream& operator<<(ostream& os, const pair<A, B>& p) { return os << '(' << p.first << ", " << p.second << ')'; }

#define all(x) x.begin(), x.end()
#define bits(x) __builtin_popcount(x)
#define int ll
#define sz(x) int((x).size())

typedef long long ll;
typedef long double ld;
typedef __float128 lld;
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> dd;

/*void random() {
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
}*/

template <class FR>
inline FR max(FR& a, const FR& b) {
    return (a > b) ? a : b;
}

template <class FR>
inline FR min(FR& a, const FR& b) {
    return (a > b) ? b : a;
}

const int MAXN = (int) 1e6 + 10;
const int MOD = (int) 1e9 + 7;
const int INF = 1e9 + 1;

struct wave {
    int l, r, a;
};

int32_t main() {
//    freopen("firesafe.in", "r", stdin), freopen("firesafe.out", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, k;
    cin >> n >> k;
    vector<wave> waves(n);
    for (int i = 0; i < n; ++i) {
        int l, r, a;
        cin >> l >> r >> a;
        waves[i] = {l, r, a};
    }
    vector<int> dp(n, INF);
    dp[0] = 0;
    int ans = INF;
    for (int i = 0; i < n; ++i) {
        int bullets = k;
        int used = dp[i];
        if (used == INF) {
            continue;
        }
        for (int j = i; j < n; ++j) {
            auto wv = waves[j];
            if (((wv.a - bullets) - (wv.r - wv.l) * k) > 0) {
                break;
            }
            int ost = 0;
            int reload = 0;
            used += wv.a;
            if (wv.a <= bullets) {
                ost = bullets - wv.a;
            } else {
                reload = (wv.a - bullets + k - 1) / k;
                ost = k - (wv.a - bullets) + (reload - 1) * k;
            }
//            cout << bullets << ' ' << wv.a << ' ' << i << ' ' << j << ' ' << ost << ' ' << reload << '\n';
            if (j + 1 < n) {
                if (waves[j].r + 1 < waves[j + 1].l)
                    dp[j + 1] = min(dp[j + 1], used + ost);
            } else {
                ans = min(ans, used);
            }
            bullets = ost;
        }
    }
    if (ans == INF) {
        return cout << -1,0;
    }
    cout << ans << '\n';
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
#define F first
#define S second
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#ifdef dremix
    #define p(x) cerr<<#x<<" = "<<x<<endl;
    #define p2(x,y) cerr<<#x<<" , "<<#y<<" = "<<x<<" , "<<y<<endl;
    #define pp(x) cerr<<#x<<" = ("<<x.F<<" , "<<x.S<<")"<<endl;
    #define pv(x) cerr<<#x<<" = {";for(auto u : x)cerr<<u<<", ";cerr<<"}"<<endl;
    #define ppv(x) cerr<<#x<<" = {";for(auto u : x)cerr<<u.F<<"-"<<u.S<<", ";cerr<<"}"<<endl;
#else
    #define p(x)
    #define p2(x,y)
    #define pp(x)
    #define pv(x)
    #define ppv(x)
#endif
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int maxp = 22;
const ld EPS = 1e-18;
const ll INF = 1e18;
const int MOD = 1e9+7;
const int N = 2005;
void yesno(bool a){if(a){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}}
ll power(ll x, ll n, ll m){if(n==0)return 1;ll u=power(x,n/2,m);u=(u*u)%m;if(n%2)u=(u*x)%m;return u;}
/// * int overflow, array bounds
/// * special cases (n=1?)
/// * do something - write observations down
/// * change approaches

/// states : dp[pos][last][start][can]

/// pos -> current wave
/// last -> last reload wave
/// start -> if reload happened at start or end of the wave
/// can -> if we can use first moment or not
ll pref[N];

struct ano{
    int l,r,x;
};

ano arr[N];

ll dp[N][N][2];
int n;
ll k;

ll solve(int pos, int last, bool can){
    if(pos==n+1)return 0;
    if(dp[pos][last][can]!=-1)
        return dp[pos][last][can];

    ll mag = k;
    mag -= (pref[pos-1] - pref[last])%k;

    ll tot = 0;
    ll kill = arr[pos].x;

    p2(pos,last)
    p2(can,mag)

    if(mag>=kill){
        mag -= kill;
    }
    else{
        tot ++;
        kill -= mag;
    }

    p2(tot,mag)
    p(kill)

    if(tot==1){
        tot += (kill+k-1) / k;
        int extra = kill%k;
        mag = k - extra;
        if(extra == 0){
            mag = 0;
            tot--;
        }
    }

    int use = arr[pos].r-arr[pos].l + can;
    if(use<=tot)return dp[pos][last][can] = INF;

    int curr = arr[pos].l + 1 - can + tot;

    p2(use,curr)

    ll used = arr[pos].x;

    bool ok = 1;
    if(mag==0){
        if(pos+1<=n && arr[pos+1].l==curr)ok = 0;
    }
    ll best = solve(pos+1,last,ok)+used;
    if(pos+1<=n && arr[pos+1].l==curr){
        p(1)
        best = min(best,solve(pos+1,pos,0) + used + mag);
    }
    else{
        best = min(best,solve(pos+1,pos,1) + used + mag);
    }
    return dp[pos][last][can] = best;
}

int main (){
fastio;

cin>>n>>k;

int i;
pref[0] = 0;

for(i=1;i<=n;i++){
    cin>>arr[i].l>>arr[i].r>>arr[i].x;
    pref[i] = pref[i-1] + arr[i].x;
}

int j;

for(i=0;i<=n;i++)for(j=0;j<=n;j++)
    for(int p=0;p<2;p++)
        dp[i][j][p] = -1;

ll res = solve(1,0,1);
if(res>=INF)cout<<-1;
else cout<<res;

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
#define F first
#define S second
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#ifdef dremix
    #define p(x) cerr<<#x<<" = "<<x<<endl;
    #define p2(x,y) cerr<<#x<<" , "<<#y<<" = "<<x<<" , "<<y<<endl;
    #define pp(x) cerr<<#x<<" = ("<<x.F<<" , "<<x.S<<")"<<endl;
    #define pv(x) cerr<<#x<<" = {";for(auto u : x)cerr<<u<<", ";cerr<<"}"<<endl;
    #define ppv(x) cerr<<#x<<" = {";for(auto u : x)cerr<<u.F<<"-"<<u.S<<", ";cerr<<"}"<<endl;
#else
    #define p(x)
    #define p2(x,y)
    #define pp(x)
    #define pv(x)
    #define ppv(x)
#endif
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int maxp = 22;
const ld EPS = 1e-18;
const ll INF = 1e18;
const int MOD = 1e9+7;
const int N = 2005;
void yesno(bool a){if(a){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}}
ll power(ll x, ll n, ll m){if(n==0)return 1;ll u=power(x,n/2,m);u=(u*u)%m;if(n%2)u=(u*x)%m;return u;}
/// * int overflow, array bounds
/// * special cases (n=1?)
/// * do something - write observations down
/// * change approaches

/// states : dp[pos][last][start][can]

/// pos -> current wave
/// last -> last reload wave
/// start -> if reload happened at start or end of the wave
/// can -> if we can use first moment or not
ll pref[N];

struct ano{
    ll l,r,x;
};

ano arr[N];

ll dp[N][N][2];
int n;
ll k;

ll solve(int pos, int last, bool can){
    if(pos==n+1)return 0;
    if(dp[pos][last][can]!=-1)
        return dp[pos][last][can];

    ll mag = k;
    mag -= (pref[pos-1] - pref[last])%k;

    ll tot = 0;
    ll kill = arr[pos].x;

    p2(pos,last)
    p2(can,mag)

    if(mag>=kill){
        mag -= kill;
    }
    else{
        tot ++;
        kill -= mag;
    }

    p2(tot,mag)
    p(kill)

    if(tot==1){
        tot += (kill) / k;
        ll extra = kill%k;
        mag -= extra;
        if(extra == 0){
            mag = 0;
            tot--;
        }
    }

    ll use = arr[pos].r-arr[pos].l + can;
    if(use<=tot)return dp[pos][last][can] = INF;

    ll curr = arr[pos].l + 1 - can + tot;

    p2(use,curr)

    ll used = arr[pos].x;

    bool ok = 1;
    if(mag==0){
        if(pos+1<=n && arr[pos+1].l==curr)ok = 0;
    }
    ll best = solve(pos+1,last,ok)+used;

    if(pos+1<=n && arr[pos+1].l==curr){
        best = min(best,solve(pos+1,pos,0) + used + mag);
    }
    else{
        best = min(best,solve(pos+1,pos,1) + used + mag);
    }
    return dp[pos][last][can] = best;
}

int main (){
fastio;

cin>>n>>k;

int i;
pref[0] = 0;

for(i=1;i<=n;i++){
    cin>>arr[i].l>>arr[i].r>>arr[i].x;
    pref[i] = pref[i-1] + arr[i].x;
}

int j;

for(i=0;i<=n;i++)for(j=0;j<=n;j++)
    for(int p=0;p<2;p++)
        dp[i][j][p] = -1;

ll res = solve(1,0,1);
if(res>=INF)cout<<-1;
else cout<<res;

/*

4 5
1 3 12
3 3 3
3 4 1
4 4 4

*/

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
#define F first
#define S second
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#ifdef dremix
    #define p(x) cerr<<#x<<" = "<<x<<endl;
    #define p2(x,y) cerr<<#x<<" , "<<#y<<" = "<<x<<" , "<<y<<endl;
    #define pp(x) cerr<<#x<<" = ("<<x.F<<" , "<<x.S<<")"<<endl;
    #define pv(x) cerr<<#x<<" = {";for(auto u : x)cerr<<u<<", ";cerr<<"}"<<endl;
    #define ppv(x) cerr<<#x<<" = {";for(auto u : x)cerr<<u.F<<"-"<<u.S<<", ";cerr<<"}"<<endl;
#else
    #define p(x)
    #define p2(x,y)
    #define pp(x)
    #define pv(x)
    #define ppv(x)
#endif
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int maxp = 22;
const ld EPS = 1e-18;
const ll INF = 1e18;
const int MOD = 1e9+7;
const int N = 2005;
void yesno(bool a){if(a){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}}
ll power(ll x, ll n, ll m){if(n==0)return 1;ll u=power(x,n/2,m);u=(u*u)%m;if(n%2)u=(u*x)%m;return u;}
/// * int overflow, array bounds
/// * special cases (n=1?)
/// * do something - write observations down
/// * change approaches

/// states : dp[pos][last][start][can]

/// pos -> current wave
/// last -> last reload wave
/// start -> if reload happened at start or end of the wave
/// can -> if we can use first moment or not
ll pref[N];

struct ano{
    ll l,r,x;
};

ano arr[N];

ll dp[N][N][2];
int n;
ll k;

ll solve(int pos, int last, bool can){
    if(pos==n+1)return 0;
    if(dp[pos][last][can]!=-1)
        return dp[pos][last][can];

    ll mag = k;
    mag -= (pref[pos-1] - pref[last])%k;

    ll tot = 0;
    ll kill = arr[pos].x;

    p2(pos,last)
    p2(can,mag)

    if(mag>=kill){
        mag -= kill;
    }
    else{
        tot ++;
        kill -= mag;
    }

    p2(tot,mag)
    p(kill)

    if(tot==1){
        tot += (kill) / k;
        ll extra = kill%k;
        mag = k - extra;
        if(extra == 0){
            mag = 0;
            tot--;
        }
    }

    ll use = arr[pos].r-arr[pos].l + can;
    if(use<=tot)return dp[pos][last][can] = INF;

    ll curr = arr[pos].l + 1 - can + tot;

    p2(use,curr)

    ll used = arr[pos].x;

    bool ok = 1;
    if(mag==0){
        if(pos+1<=n && arr[pos+1].l==curr)ok = 0;
    }
    ll best = solve(pos+1,last,ok)+used;
    if(pos+1<=n && arr[pos+1].l==curr){
        p(1)
        best = min(best,solve(pos+1,pos,0) + used + mag);
    }
    else{
        best = min(best,solve(pos+1,pos,1) + used + mag);
    }
    return dp[pos][last][can] = best;
}

int main (){
fastio;

cin>>n>>k;

int i;
pref[0] = 0;

for(i=1;i<=n;i++){
    cin>>arr[i].l>>arr[i].r>>arr[i].x;
    pref[i] = pref[i-1] + arr[i].x;
}

int j;

for(i=0;i<=n;i++)for(j=0;j<=n;j++)
    for(int p=0;p<2;p++)
        dp[i][j][p] = -1;

ll res = solve(1,0,1);
if(res>=INF/2)cout<<-1;
else cout<<res;

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
#define F first
#define S second
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#ifdef dremix
    #define p(x) cerr<<#x<<" = "<<x<<endl;
    #define p2(x,y) cerr<<#x<<" , "<<#y<<" = "<<x<<" , "<<y<<endl;
    #define pp(x) cerr<<#x<<" = ("<<x.F<<" , "<<x.S<<")"<<endl;
    #define pv(x) cerr<<#x<<" = {";for(auto u : x)cerr<<u<<", ";cerr<<"}"<<endl;
    #define ppv(x) cerr<<#x<<" = {";for(auto u : x)cerr<<u.F<<"-"<<u.S<<", ";cerr<<"}"<<endl;
#else
    #define p(x)
    #define p2(x,y)
    #define pp(x)
    #define pv(x)
    #define ppv(x)
#endif
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int maxp = 22;
const ld EPS = 1e-18;
const ll INF = 1e18;
const int MOD = 1e9+7;
const int N = 2005;
void yesno(bool a){if(a){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}}
ll power(ll x, ll n, ll m){if(n==0)return 1;ll u=power(x,n/2,m);u=(u*u)%m;if(n%2)u=(u*x)%m;return u;}
/// * int overflow, array bounds
/// * special cases (n=1?)
/// * do something - write observations down
/// * change approaches

/// states : dp[pos][last][start][can]

/// pos -> current wave
/// last -> last reload wave
/// start -> if reload happened at start or end of the wave
/// can -> if we can use first moment or not
ll pref[N];

struct ano{
    ll l,r,x;
};

ano arr[N];

ll dp[N][N][2];
int n;
ll k;

ll solve(int pos, int last, bool can){
    if(pos==n+1)return 0;
    if(dp[pos][last][can]!=-1)
        return dp[pos][last][can];

    ll mag = k;
    mag -= (pref[pos-1] - pref[last])%k;

    ll tot = 0;
    ll kill = arr[pos].x;

    p2(pos,last)
    p2(can,mag)

    if(mag>=kill){
        mag -= kill;
    }
    else{
        tot ++;
        kill -= mag;
    }

    p2(tot,mag)
    p(kill)

    if(tot==1){
        tot += (kill) / k;
        ll extra = kill%k;
        mag = k - extra;
        if(extra == 0){
            mag = 0;
            tot--;
        }
    }

    ll use = arr[pos].r-arr[pos].l + can;
    if(use<=tot)return dp[pos][last][can] = INF;

    ll curr = arr[pos].l + 1 - can + tot;

    p2(use,curr)

    ll used = arr[pos].x;

    bool ok = 1;
    if(mag==0){
        if(pos+1<=n && arr[pos+1].l==curr)ok = 0;
    }
    ll best = solve(pos+1,last,ok)+used;

    if(pos+1<=n && arr[pos+1].l==curr){
        best = min(best,solve(pos+1,pos,0) + used + mag);
    }
    else{
        best = min(best,solve(pos+1,pos,1) + used + mag);
    }
    return dp[pos][last][can] = best;
}

int main (){
fastio;

cin>>n>>k;

int i;
pref[0] = 0;

for(i=1;i<=n;i++){
    cin>>arr[i].l>>arr[i].r>>arr[i].x;
    pref[i] = pref[i-1] + arr[i].x;
}

int j;

for(i=0;i<=n;i++)for(j=0;j<=n;j++)
    for(int p=0;p<2;p++)
        dp[i][j][p] = -1;

ll res = solve(1,0,1);
if(res>=INF)cout<<-1;
else cout<<res;

/*

4 5
1 3 12
3 3 3
3 4 1
4 4 4

10 89
1 2 82
2 2 31
3 4 63
6 7 18
9 9 44
10 11 95
13 13 52
13 15 39
15 16 70
17 18 54

*/

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
#define F first
#define S second
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#ifdef dremix
    #define p(x) cerr<<#x<<" = "<<x<<endl;
    #define p2(x,y) cerr<<#x<<" , "<<#y<<" = "<<x<<" , "<<y<<endl;
    #define pp(x) cerr<<#x<<" = ("<<x.F<<" , "<<x.S<<")"<<endl;
    #define pv(x) cerr<<#x<<" = {";for(auto u : x)cerr<<u<<", ";cerr<<"}"<<endl;
    #define ppv(x) cerr<<#x<<" = {";for(auto u : x)cerr<<u.F<<"-"<<u.S<<", ";cerr<<"}"<<endl;
#else
    #define p(x)
    #define p2(x,y)
    #define pp(x)
    #define pv(x)
    #define ppv(x)
#endif
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int maxp = 22;
const ld EPS = 1e-18;
const ll INF = 1e18;
const int MOD = 1e9+7;
const int N = 2005;
void yesno(bool a){if(a){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}}
ll power(ll x, ll n, ll m){if(n==0)return 1;ll u=power(x,n/2,m);u=(u*u)%m;if(n%2)u=(u*x)%m;return u;}
/// * int overflow, array bounds
/// * special cases (n=1?)
/// * do something - write observations down
/// * change approaches

/// states : dp[pos][last][start][can]

/// pos -> current wave
/// last -> last reload wave
/// start -> if reload happened at start or end of the wave
/// can -> if we can use first moment or not
ll pref[N];

struct ano{
    ll l,r,x;
};

ano arr[N];

ll dp[N][N][2];
int n;
ll k;

ll solve(int pos, int last, bool can){
    if(pos==n+1)return 0;
    if(dp[pos][last][can]!=-1)
        return dp[pos][last][can];

    ll mag = k;
    mag -= (pref[pos-1] - pref[last])%k;

    ll tot = 0;
    ll kill = arr[pos].x;

    p2(pos,last)
    p2(can,mag)

    if(mag>=kill){
        mag -= kill;
    }
    else{
        tot ++;
        kill -= mag;
    }

    p2(tot,mag)
    p(kill)

    if(tot==1){
        tot += (kill+k-1) / k;
        ll extra = kill%k;
        mag = k - extra;
        if(extra == 0){
            mag = 0;
            tot--;
        }
    }

    ll use = arr[pos].r-arr[pos].l + can;
    if(use<=tot)return dp[pos][last][can] = INF;

    ll curr = arr[pos].l + 1 - can + tot;

    p2(use,curr)

    ll used = arr[pos].x;

    bool ok = 1;
    if(mag==0){
        if(pos+1<=n && arr[pos+1].l==curr)ok = 0;
    }
    ll best = solve(pos+1,last,ok)+used;
    if(pos+1<=n && arr[pos+1].l==curr){
        p(1)
        best = min(best,solve(pos+1,pos,0) + used + mag);
    }
    else{
        best = min(best,solve(pos+1,pos,1) + used + mag);
    }
    return dp[pos][last][can] = best;
}

int main (){
fastio;

cin>>n>>k;

int i;
pref[0] = 0;

for(i=1;i<=n;i++){
    cin>>arr[i].l>>arr[i].r>>arr[i].x;
    pref[i] = pref[i-1] + arr[i].x;
}

int j;

for(i=0;i<=n;i++)for(j=0;j<=n;j++)
    for(int p=0;p<2;p++)
        dp[i][j][p] = -1;

ll res = solve(1,0,1);
if(res>=INF)cout<<-1;
else cout<<res;

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
#define F first
#define S second
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#ifdef dremix
    #define p(x) cerr<<#x<<" = "<<x<<endl;
    #define p2(x,y) cerr<<#x<<" , "<<#y<<" = "<<x<<" , "<<y<<endl;
    #define pp(x) cerr<<#x<<" = ("<<x.F<<" , "<<x.S<<")"<<endl;
    #define pv(x) cerr<<#x<<" = {";for(auto u : x)cerr<<u<<", ";cerr<<"}"<<endl;
    #define ppv(x) cerr<<#x<<" = {";for(auto u : x)cerr<<u.F<<"-"<<u.S<<", ";cerr<<"}"<<endl;
#else
    #define p(x)
    #define p2(x,y)
    #define pp(x)
    #define pv(x)
    #define ppv(x)
#endif
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int maxp = 22;
const ld EPS = 1e-18;
const ll INF = 1e18;
const int MOD = 1e9+7;
const int N = 2005;
void yesno(bool a){if(a){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}}
ll power(ll x, ll n, ll m){if(n==0)return 1;ll u=power(x,n/2,m);u=(u*u)%m;if(n%2)u=(u*x)%m;return u;}
/// * int overflow, array bounds
/// * special cases (n=1?)
/// * do something - write observations down
/// * change approaches

/// states : dp[pos][last][start][can]

/// pos -> current wave
/// last -> last reload wave
/// start -> if reload happened at start or end of the wave
/// can -> if we can use first moment or not
ll pref[N];

struct ano{
    ll l,r,x;
};

ano arr[N];

ll dp[N][N][2];
int n;
ll k;

ll solve(int pos, int last, bool can){
    if(pos==n+1)return 0;
    if(dp[pos][last][can]!=-1)
        return dp[pos][last][can];

    ll mag = k;
    mag -= (pref[pos-1] - pref[last])%k;

    ll tot = 0;
    ll kill = arr[pos].x;

    p2(pos,last)
    p2(can,mag)

    if(mag>=kill){
        mag -= kill;
    }
    else{
        tot ++;
        kill -= mag;
    }

    p2(tot,mag)
    p(kill)

    if(tot==1){
        tot += (kill+k-1) / k;
        ll extra = kill%k;
        mag = k - extra;
        if(extra == 0){
            mag = 0;
            tot--;
        }
    }

    ll use = arr[pos].r-arr[pos].l + can;
    if(use<=tot)return dp[pos][last][can] = INF;

    ll curr = arr[pos].l + 1 - can + tot;

    p2(use,curr)

    ll used = arr[pos].x;

    bool ok = 1;
    if(mag==0){
        if(pos+1<=n && arr[pos+1].l==curr)ok = 0;
    }
    ll best = solve(pos+1,last,ok)+used;
    if(pos+1<=n && arr[pos+1].l==curr){
        p(1)
        best = min(best,solve(pos+1,pos,0) + used + mag);
    }
    else{
        best = min(best,solve(pos+1,pos,1) + used + mag);
    }
    return dp[pos][last][can] = best;
}

int main (){
fastio;

cin>>n>>k;

int i;
pref[0] = 0;

for(i=1;i<=n;i++){
    cin>>arr[i].l>>arr[i].r>>arr[i].x;
    pref[i] = pref[i-1] + arr[i].x;
}

int j;

for(i=0;i<=n;i++)for(j=0;j<=n;j++)
    for(int p=0;p<2;p++)
        dp[i][j][p] = -1;

ll res = solve(1,0,1);
if(res>=INF/2)cout<<-1;
else cout<<res;

}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<ll> vl ;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef vector<pair<int,int> > vpii;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define max(a,b) (a>b ? a : b)
#define min(a,b) (a<b ? a : b)
#define FAST ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)

const ll MOD = (ll)7+(ll)1e9 ;
const ll N = 1000005;
const double PI =4*atan(1);
const double eps = 1e-10;
const long long oo = 1e10;
const int K = 26;

int n;
ll k;
ll l[N];
ll r[N];
ll a[N];
ll dp[2005];

ll solve(int u, int flag = 1){
    if(u>=n)return 0;
    if(dp[u]>=0)return dp[u];
    ll time = r[u]-l[u];
    ll needed = (a[u]+k-1)/k;
    ll real = needed-flag;
    if(time<real){
        return dp[u]=1e18;
    }
    ll to=1e18;
    if(u==n-1){
        to=min(to,a[u]);
    }else{
        to=needed*k+solve(u+1,l[u+1]>l[u]+real);
    }
    ll cur = needed*k-a[u];
    //cout << endl;
    for(int j=u+1;j<n;j++){
        //cout << u << " " << j << " " << cur << endl;
        if(a[j]<=cur){
            cur-=a[j];
            if(j<n-1){
                to=min(to,needed*k+solve(j+1,l[j+1]>l[j]));
            }else{
                to=min(to,needed*k-cur);
            }

        }else{
            ll val = a[j]-cur;
            ll needed2 = (val+k-1)/k;
            time = r[j]-l[j];
            if(time<needed2){
                break;
            }
            needed+=needed2;
            cur = needed2*k-val;
            if(j<n-1)
            to=min(to,needed*k+solve(j+1,l[j+1]>l[j]+needed2));
            else to=min(to,needed*k-cur);
        }
    }
    return dp[u]=to;

}

int main(){
    FAST;
   // freopen("easy.txt","r",stdin);
    memset(dp,-1,sizeof(dp));
    cin >> n >> k;
    for(int i=0;i<n;i++){
        cin >> l[i] >> r[i] >> a[i] ;
    }
    ll ans=solve(0);
    if(ans<1e18){
        cout << ans;
    }else{
        cout << -1;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<ll> vl ;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef vector<pair<int,int> > vpii;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define max(a,b) (a>b ? a : b)
#define min(a,b) (a<b ? a : b)
#define FAST ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)

const ll MOD = (ll)7+(ll)1e9 ;
const ll N = 1000005;
const double PI =4*atan(1);
const double eps = 1e-10;
const long long oo = 1e10;
const int K = 26;

int n;
ll k;
ll l[N];
ll r[N];
ll a[N];
ll dp[2005];

ll solve(int u, int flag = 1){
    if(u>=n)return 0;
    if(dp[u]>=0)return dp[u];
    ll time = r[u]-l[u];
    ll needed = (a[u]+k-1)/k;
    ll real = needed-flag;
    if(time<real){
        return dp[u]=1e18;
    }
    ll to=1e18;
    if(u==n-1){
        to=min(to,a[u]);
    }else{
        to=needed*k+solve(u+1,l[u+1]>r[u]);
    }
    ll cur = needed*k-a[u];
    //cout << endl;
    for(int j=u+1;j<n;j++){
        //cout << u << " " << j << " " << cur << endl;
        if(a[j]<=cur){
            cur-=a[j];
            if(j<n-1){
                to=min(to,needed*k+solve(j+1,l[j+1]>r[j]));
            }else{
                to=min(to,needed*k-cur);
            }

        }else{
            ll val = a[j]-cur;
            ll needed2 = (val+k-1)/k;
            time = r[j]-l[j];
            if(time<needed2){
                break;
            }
            needed+=needed2;
            cur = val - needed2*k;
            if(j<n-1)
            to=min(to,needed*k+solve(j+1,l[j+1]>r[j]));
            else to=min(to,needed*k-cur);
        }
    }
    return dp[u]=to;

}

int main(){
    FAST;
    //freopen("easy.txt","r",stdin);
    memset(dp,-1,sizeof(dp));
    cin >> n >> k;
    for(int i=0;i<n;i++){
        cin >> l[i] >> r[i] >> a[i] ;
    }
    ll ans=solve(0);
    if(ans<1e18){
        cout << ans;
    }else{
        cout << -1;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<ll> vl ;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef vector<pair<int,int> > vpii;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define max(a,b) (a>b ? a : b)
#define min(a,b) (a<b ? a : b)
#define FAST ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)

const ll MOD = (ll)7+(ll)1e9 ;
const ll N = 1000005;
const double PI =4*atan(1);
const double eps = 1e-10;
const long long oo = 1e10;
const int K = 26;

int n;
ll k;
ll l[N];
ll r[N];
ll a[N];
ll dp[2005];

ll solve(int u, int flag = 1){
    if(u>=n)return 0;
    if(dp[u]>=0)return dp[u];
    ll time = r[u]-l[u];
    ll needed = (a[u]+k-1)/k;
    ll real = needed-flag;
    if(time<real){
        return dp[u]=1e18;
    }
    ll to=1e18;
    if(u==n-1){
        to=min(to,a[u]);
    }else{
        to=needed*k+solve(u+1,l[u+1]>l[u]+real);
    }
    ll cur = needed*k-a[u];
    //cout << endl;
    for(int j=u+1;j<n;j++){
        //cout << u << " " << j << " " << cur << endl;
        if(a[j]<=cur){
            cur-=a[j];
            if(j<n-1){
                to=min(to,needed*k+solve(j+1,l[j+1]>l[j]));
            }else{
                to=min(to,needed*k-cur);
            }

        }else{
            ll val = a[j]-cur;
            ll needed2 = (val+k-1)/k;
            time = r[j]-l[j];
            if(time<needed2){
                break;
            }
            needed+=needed2;
            cur = val - needed2*k;
            if(j<n-1)
            to=min(to,needed*k+solve(j+1,l[j+1]>l[j]+needed2));
            else to=min(to,needed*k-cur);
        }
    }
    return dp[u]=to;

}

int main(){
    FAST;
    //freopen("easy.txt","r",stdin);
    memset(dp,-1,sizeof(dp));
    cin >> n >> k;
    for(int i=0;i<n;i++){
        cin >> l[i] >> r[i] >> a[i] ;
    }
    ll ans=solve(0);
    if(ans<1e18){
        cout << ans;
    }else{
        cout << -1;
    }
    return 0;
}


#pragma GCC target ("avx2")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("O3")
#include "bits/stdc++.h"
#include <unordered_set>
#include <unordered_map>
#include <random>
using namespace std;
typedef long long ll;
const ll MOD = 1'000'000'007LL; /*998'244'353LL;*/
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
const int dx[4]={ 1,0,-1,0 };
const int dy[4]={ 0,1,0,-1 };

int N;
ll K;
ll L[2001], R[2000], A[2000];
ll S[2001];

const ll INF = 2e18L;

ll dp[2001][2001];
ll solve(int i, int b, ll k){
    if(i == N) return 0;
    if(dp[i][b] != -1) return dp[i][b];
    ll a = A[i];
    a -= min(k, A[i]);
    k -= min(k, A[i]);
    ll t = (a+K-1)/K;
    k = k+t*K-a;
    if(t > R[i]-L[i]){
        return dp[i][b] = INF;
    }
    if(t == R[i]-L[i] && R[i] == L[i+1]){
        return dp[i][b] = A[i] + solve(i+1, b, k);
    }
    else{
        return dp[i][b] = min(A[i] + solve(i+1, b, k), A[i]+k + solve(i+1, i+1, K));
    }
}

signed main(){
    cin >> N >> K;
    rep(i, N) cin >> L[i] >> R[i] >> A[i];
    L[N] = INF;

    rep(i, N) S[i+1] = S[i] + A[i];
    memset(dp, -1, sizeof(dp));
    ll ans = solve(0, 0, K);
    if(ans < INF) cout << ans << endl;
    else cout << -1 << endl;
}


#pragma GCC target ("avx2")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("O3")
#include "bits/stdc++.h"
#include <unordered_set>
#include <unordered_map>
#include <random>
using namespace std;
typedef long long ll;
const ll MOD = 1'000'000'007LL; /*998'244'353LL;*/
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
const int dx[4]={ 1,0,-1,0 };
const int dy[4]={ 0,1,0,-1 };

int N;
ll K;
ll L[2000], R[2000], A[2000];
ll S[2001];

const ll INF = 2e18L;

ll dp[2001][2001];
ll solve(int i, int b){
    if(i == N) return 0;
    if(dp[i][b] != -1) return dp[i][b];
    ll k = ((-(S[i]-S[b]))%K+K)%K;
    if(i == b) k = K;
    ll a = A[i];
    a -= min(k, A[i]);
    k -= min(k, A[i]);
    ll t = (a+K-1)/K;
    if(t > R[i]-L[i]){
        return dp[i][b] = INF;
    }
    if(t == R[i]-L[i]){
        return dp[i][b] = A[i] + solve(i+1, b);
    }
    else{
        return dp[i][b] = min(A[i] + solve(i+1, b), ((-(S[i]-S[b]))%K+K)%K + (t+1)*K + solve(i+1, i+1));
    }
}

signed main(){
    cin >> N >> K;
    rep(i, N) cin >> L[i] >> R[i] >> A[i];

    ll k = K;
    rep(i, N){
        ll a = A[i];
        a -= min(k, A[i]);
        k -= min(k, A[i]);
        ll t = (a+K-1)/K;
        if(t > R[i]-L[i]){
            cout << -1 << endl;
            return 0;
        }
        if(t == R[i]-L[i]){
            k = t*K-a;
        }
        else{
            k = K;
        }
    }

    rep(i, N) S[i+1] = S[i] + A[i];
    memset(dp, -1, sizeof(dp));
    cout << solve(0, 0) << endl;
}


#pragma GCC target ("avx2")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("O3")
#include "bits/stdc++.h"
#include <unordered_set>
#include <unordered_map>
#include <random>
using namespace std;
typedef long long ll;
const ll MOD = 1'000'000'007LL; /*998'244'353LL;*/
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
const int dx[4]={ 1,0,-1,0 };
const int dy[4]={ 0,1,0,-1 };

int N;
ll K;
ll L[2000], R[2000], A[2000];
ll S[2001];

const ll INF = 2e18L;

ll dp[2001][2001];
ll solve(int i, int b, ll k){
    if(i == N) return 0;
    if(dp[i][b] != -1) return dp[i][b];
    ll a = A[i];
    a -= min(k, A[i]);
    k -= min(k, A[i]);
    ll t = (a+K-1)/K;
    k = k+t*K-a;
    if(t > R[i]-L[i]){
        return dp[i][b] = INF;
    }
    if(t == R[i]-L[i]){
        return dp[i][b] = A[i] + solve(i+1, b, k);
    }
    else{
        return dp[i][b] = min(A[i] + solve(i+1, b, k), A[i]+k + solve(i+1, i+1, K));
    }
}

signed main(){
    cin >> N >> K;
    rep(i, N) cin >> L[i] >> R[i] >> A[i];

    rep(i, N) S[i+1] = S[i] + A[i];
    memset(dp, -1, sizeof(dp));
    ll ans = solve(0, 0, K);
    if(ans < INF) cout << ans << endl;
    else cout << -1 << endl;
}


#pragma GCC target ("avx2")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("O3")
#include "bits/stdc++.h"
#include <unordered_set>
#include <unordered_map>
#include <random>
using namespace std;
typedef long long ll;
const ll MOD = 1'000'000'007LL; /*998'244'353LL;*/
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
const int dx[4]={ 1,0,-1,0 };
const int dy[4]={ 0,1,0,-1 };

int N;
ll K;
ll L[2000], R[2000], A[2000];
ll S[2001];

const ll INF = 2e18L;

ll dp[2001][2001];
ll solve(int i, int b){
    if(i == N) return 0;
    if(dp[i][b] != -1) return dp[i][b];

    ll k = ((-(S[i]-S[b]))%K+K)%K;
    if(i == b) k = K;
    ll memok = k;
    ll a = A[i];
    a -= min(k, A[i]);
    k -= min(k, A[i]);
    ll t = (a+K-1)/K;
    if(t > R[i]-L[i]){
        return dp[i][b] = INF;
    }
    if(t == R[i]-L[i]){
        return dp[i][b] = A[i] + solve(i+1, b);
    }
    else{
        return dp[i][b] = min(A[i] + solve(i+1, b), memok + t*K + solve(i+1, i+1));
    }
}

signed main(){
    cin >> N >> K;
    rep(i, N) cin >> L[i] >> R[i] >> A[i];

    rep(i, N) S[i+1] = S[i] + A[i];
    memset(dp, -1, sizeof(dp));
    ll ans = solve(0, 0);
    if(ans < INF) cout << ans << endl;
    else cout << -1 << endl;
}


#include<iostream>
#include<cmath>
#include<vector>
#include<string>
#include<set>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>

using namespace std;

typedef long double ld;
typedef long long ll;
typedef pair<ll, ll> pi;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<pi> vpi;
typedef vector<vpi> vvpi;
typedef map<ll, ll> mii;
typedef set<ll> si;
typedef multiset<ll> msi;
typedef vector<si> vsi;
typedef vector<string> vs;

template <typename T> using V = vector<T>;
template <typename T> using VV = vector<V<T>>;
template < typename T >
istream& operator>> (istream& cin, V<T>& arr) {
	for (T& val : arr) {
		cin >> val;
	}
	return cin;
}
template < typename T >
ostream& operator<< (ostream& cout, V<T>& arr) {
	for (T& val : arr) {
		cout << val << " ";
	}
	return cout;
}

struct wave
{
	ll l, r, a;
};

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cout.precision(10);

	ll n, k;
	cin >> n >> k;

	V<wave> arr(n);
	for (int i = 0; i < n; i++) {
		cin >> arr[i].l >> arr[i].r >> arr[i].a;
	}

	vvi dp(n+1, vi(n+1, -1));
	dp[0][0] = 0;

	for (int i = 1; i <= n; i++) {
		wave w = arr[i-1];
		ll locans = -1;
		for (int j = 0; j < i; j++) {
			if (dp[i - 1][j] == -1) {
				continue;
			}
			ll ost = (k - dp[i - 1][j] % k)%k;
			if (j == i - 1) {
				ost = k;
			}
			ll time = w.r - w.l;
			ll enemy = w.a - ost;
			if (time * k < enemy) {
				continue;
			}
			dp[i][j] = dp[i - 1][j] + w.a;

			if (i != n) {
				ll worktime= 0;
				if (enemy > 0) {
					worktime = enemy / k;
					if (enemy % k != 0) {
						worktime++;
					}
				}
				if (worktime < time && time!= 0 ||
					w.r != arr[i].l) {
					if (locans == -1 || dp[i][j] < locans) {
						locans = dp[i][j];
					}
				}
			}

		}
		if (i != n && locans != -1) {
			ll ans = locans / k * k + k;
			dp[i][i] = ans;
		}
	}
	ll ans = -1;
	for (int i = 0; i < n; i++) {
		if (ans == -1 || (dp[n][i] < ans && dp[n][i] != -1)) {
			ans = dp[n][i];
		}
	}

	cout << ans;

	return 0;
}


#include<iostream>
#include<cmath>
#include<vector>
#include<string>
#include<set>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>

using namespace std;

typedef long double ld;
typedef long long ll;
typedef pair<ll, ll> pi;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<pi> vpi;
typedef vector<vpi> vvpi;
typedef map<ll, ll> mii;
typedef set<ll> si;
typedef multiset<ll> msi;
typedef vector<si> vsi;
typedef vector<string> vs;

template <typename T> using V = vector<T>;
template <typename T> using VV = vector<V<T>>;
template < typename T >
istream& operator>> (istream& cin, V<T>& arr) {
	for (T& val : arr) {
		cin >> val;
	}
	return cin;
}
template < typename T >
ostream& operator<< (ostream& cout, V<T>& arr) {
	for (T& val : arr) {
		cout << val << " ";
	}
	return cout;
}

struct wave
{
	ll l, r, a;
};

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cout.precision(10);

	ll n, k;
	cin >> n >> k;

	V<wave> arr(n);
	for (int i = 0; i < n; i++) {
		cin >> arr[i].l >> arr[i].r >> arr[i].a;
	}

	vvi dp(n+1, vi(n+1, -1));
	dp[0][0] = 0;

	for (int i = 1; i <= n; i++) {
		wave w = arr[i-1];
		ll locans = -1;
		for (int j = 0; j < i; j++) {
			if (dp[i - 1][j] == -1) {
				continue;
			}
			ll ost = (k - dp[i - 1][j] % k)%k;
			if (j == i - 1) {
				ost = k;
			}
			ll time = w.r - w.l;
			ll enemy = w.a - ost;
			if (time * k < enemy) {
				continue;
			}
			dp[i][j] = dp[i - 1][j] + w.a;

			if (i != n) {
				ll worktime= 0;
				if (enemy > 0) {
					worktime = enemy / k;
					if (enemy % k != 0) {
						worktime++;
					}
				}
				if (worktime < time && time!= 0 ||
					w.r != arr[i].l) {
					if (locans == -1 || dp[i][j] < locans) {
						locans = dp[i][j];
					}
				}
			}

		}
		if (i != n && locans != -1) {
			ll ans = (locans-1) / k * k + k;
			dp[i][i] = ans;
		}
	}
	ll ans = -1;
	for (int i = 0; i < n; i++) {
		if (ans == -1 || (dp[n][i] < ans && dp[n][i] != -1)) {
			ans = dp[n][i];
		}
	}

	cout << ans;

	return 0;
}


#include<iostream>
#include<cmath>
#include<vector>
#include<string>
#include<set>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>

using namespace std;

typedef long double ld;
typedef long long ll;
typedef pair<ll, ll> pi;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<pi> vpi;
typedef vector<vpi> vvpi;
typedef map<ll, ll> mii;
typedef set<ll> si;
typedef multiset<ll> msi;
typedef vector<si> vsi;
typedef vector<string> vs;

template <typename T> using V = vector<T>;
template <typename T> using VV = vector<V<T>>;
template < typename T >
istream& operator>> (istream& cin, V<T>& arr) {
	for (T& val : arr) {
		cin >> val;
	}
	return cin;
}
template < typename T >
ostream& operator<< (ostream& cout, V<T>& arr) {
	for (T& val : arr) {
		cout << val << " ";
	}
	return cout;
}

struct wave
{
	ll l, r, a;
};

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cout.precision(10);

	ll n, k;
	cin >> n >> k;

	V<wave> arr(n);
	for (int i = 0; i < n; i++) {
		cin >> arr[i].l >> arr[i].r >> arr[i].a;
	}

	vvi dp(n+1, vi(n+1, -1));
	dp[0][0] = 0;

	for (int i = 1; i <= n; i++) {
		wave w = arr[i-1];
		ll locans = -1;
		for (int j = 0; j < i; j++) {
			if (dp[i - 1][j] == -1) {
				continue;
			}
			ll ost = (k - dp[i - 1][j] % k)%k;
			if (j == i - 1) {
				ost = k;
			}
			ll time = w.r - w.l;
			ll enemy = w.a - ost;
			if (time * k < enemy) {
				continue;
			}
			dp[i][j] = dp[i - 1][j] + w.a;

			if (i != n) {
				if ((enemy - 1) / k + 1 < time && time!= 0 ||
					w.r != arr[i].l) {
					if (locans == -1 || dp[i][j] < locans) {
						locans = dp[i][j];
					}
				}
			}

		}
		if (i != n && locans != -1) {
			ll ans = locans / k * k + k;
			dp[i][i] = ans;
		}
	}
	ll ans = -1;
	for (int i = 0; i < n; i++) {
		if (ans == -1 || (dp[n][i] < ans && dp[n][i] != -1)) {
			ans = dp[n][i];
		}
	}

	cout << ans;

	return 0;
}


#include<iostream>
#include<cmath>
#include<vector>
#include<string>
#include<set>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>

using namespace std;

typedef long double ld;
typedef long long ll;
typedef pair<ll, ll> pi;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<pi> vpi;
typedef vector<vpi> vvpi;
typedef map<ll, ll> mii;
typedef set<ll> si;
typedef multiset<ll> msi;
typedef vector<si> vsi;
typedef vector<string> vs;

template <typename T> using V = vector<T>;
template <typename T> using VV = vector<V<T>>;
template < typename T >
istream& operator>> (istream& cin, V<T>& arr) {
	for (T& val : arr) {
		cin >> val;
	}
	return cin;
}
template < typename T >
ostream& operator<< (ostream& cout, V<T>& arr) {
	for (T& val : arr) {
		cout << val << " ";
	}
	return cout;
}

struct wave
{
	ll l, r, a;
};

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cout.precision(10);

	ll n, k;
	cin >> n >> k;

	V<wave> arr(n);
	for (int i = 0; i < n; i++) {
		cin >> arr[i].l >> arr[i].r >> arr[i].a;
	}

	vvi dp(n+1, vi(n+1, -1));
	dp[0][0] = 0;

	for (int i = 1; i <= n; i++) {
		wave w = arr[i-1];
		ll locans = -1;
		for (int j = 0; j < i; j++) {
			if (dp[i - 1][j] == -1) {
				continue;
			}
			ll ost = (k - dp[i - 1][j] % k)%k;
			if (j == i - 1) {
				ost = k;
			}
			ll time = w.r - w.l;
			ll enemy = w.a - ost;
			if (time * k < enemy) {
				continue;
			}
			dp[i][j] = dp[i - 1][j] + w.a;
			if (locans == -1 || dp[i][j] < locans) {
				locans = dp[i][j];
			}
		}
		if (i != n && w.r != arr[i].l) {
			ll ans = locans / k * k + k;
			dp[i][i] = ans;
		}
	}
	ll ans = -1;
	for (int i = 0; i < n; i++) {
		if (ans == -1 || (dp[n][i] < ans && dp[n][i] != -1)) {
			ans = dp[n][i];
		}
	}

	cout << ans;

	return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rrep(i,n) for (int i = (n)-1; i >= 0; --i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;

int main() {
    ll n, k;
    cin >> n >> k;
    vector<ll> l(n), r(n), a(n);
    rep(i, n) cin >> l[i] >> r[i] >> a[i];
    vector<ll> req(n);
    ll req_now = 0;
    rrep(i, n) {
        req_now += a[i];
        req_now -= (r[i] - l[i]) * k;
        chmax(req_now, 0LL);
        if(req_now > k) {
            cout << -1 << endl;
            return 0;
        }
        req[i] = req_now;
    }
    ll ans = 0;
    ll now = k;
    ll time = 0;
    rep(i, n) {
        time = l[i];
        ll used = min(a[i], now);
        now -= used;
        a[i] -= used;
        ans += used;
        if (a[i]) {
            ll c = (a[i] + k - 1) / k;
            time += c;
            ans += a[i];
            now = c * k - a[i];
        }
        if (i + 1 < n && now < req[i+1]) {
            if (time >= l[i+1]) {
                cout << -1 << endl;
                return 0;
            }
            ans += now;
            now = k;
        }
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rrep(i,n) for (int i = (n)-1; i >= 0; --i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;

int main() {
    ll n, k;
    cin >> n >> k;
    vector<ll> l(n), r(n), a(n);
    rep(i, n) cin >> l[i] >> r[i] >> a[i];
    vector<ll> req(n);
    ll req_now = 0;
    rrep(i, n) {
        if (i + 1 < n && r[i] < l[i+1]) req_now = 0;
        req_now += a[i];
        req_now -= (r[i] - l[i]) * k;
        chmax(req_now, 0LL);
        if(req_now > k) {
            cout << -1 << endl;
            return 0;
        }
        req[i] = req_now;
    }
    ll ans = 0;
    ll now = k;
    ll time = 0;
    rep(i, n) {
        time = l[i];
        ll used = min(a[i], now);
        now -= used;
        a[i] -= used;
        ans += used;
        if (a[i]) {
            ll c = (a[i] + k - 1) / k;
            time += c;
            ans += a[i];
            now = c * k - a[i];
        }
        if (i + 1 < n && now < req[i+1]) {
            if (time >= l[i+1]) {
                cout << -1 << endl;
                return 0;
            }
            ans += now;
            now = k;
        }
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rrep(i,n) for (int i = (n)-1; i >= 0; --i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;

int main() {
    ll n, k;
    cin >> n >> k;
    vector<ll> l(n), r(n), a(n);
    rep(i, n) cin >> l[i] >> r[i] >> a[i];
    vector<ll> req(n);
    ll req_now = 0;
    rrep(i, n) {
        req_now += a[i];
        req_now -= (r[i] - l[i]) * k;
        chmax(req_now, 0);
        if(req_now > k) {
            cout << -1 << endl;
            return 0;
        }
        req[i] = req_now;
    }
    ll ans = 0;
    ll now = k;
    ll time = 0;
    rep(i, n) {
        ll used = min(a[i], now);
        now -= used;
        a[i] -= used;
        ans += used;
        if (a[i]) {
            ll c = (a[i] + k - 1) / k;
            time += c;
            ans += a[i];
            now = c * k - a[i];
        }
        if (i + 1 < n && now < req[i+1]) {
            if (time >= l[i+1]) {
                cout << -1 << endl;
                return 0;
            }
            ans += now;
            now = k;
        }
        if (i + 1 < n) time = l[i+1];
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rrep(i,n) for (int i = (n)-1; i >= 0; --i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;

int main() {
    ll n, k;
    cin >> n >> k;
    vector<ll> l(n), r(n), a(n);
    rep(i, n) cin >> l[i] >> r[i] >> a[i];
    vector<ll> req(n);
    int req_now = 0;
    rrep(i, n) {
        req_now += a[i];
        req_now -= (r[i] - l[i]) * k;
        chmax(req_now, 0);
        if(req_now > k) {
            cout << -1 << endl;
            return 0;
        }
        req[i] = req_now;
    }
    ll ans = 0;
    ll now = k;
    ll time = 0;
    rep(i, n) {
        ll used = min(a[i], now);
        now -= used;
        a[i] -= used;
        ans += used;
        if (a[i]) {
            ll c = (a[i] + k - 1) / k;
            time += c;
            ans += a[i];
            now = c * k - a[i];
        }
        if (i + 1 < n && now < req[i+1]) {
            if (time >= l[i+1]) {
                cout << -1 << endl;
                return 0;
            }
            ans += now;
            now = k;
        }
        if (i + 1 < n) time = l[i+1];
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rrep(i,n) for (int i = (n)-1; i >= 0; --i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;

int main() {
    ll n, k;
    cin >> n >> k;
    vector<ll> l(n), r(n), a(n);
    rep(i, n) cin >> l[i] >> r[i] >> a[i];
    vector<ll> req(n);
    ll req_now = 0;
    rrep(i, n) {
        req_now += a[i];
        req_now -= (r[i] - l[i]) * k;
        chmax(req_now, 0LL);
        if(req_now > k) {
            cout << -1 << endl;
            return 0;
        }
        req[i] = req_now;
    }
    ll ans = 0;
    ll now = k;
    ll time = 0;
    rep(i, n) {
        ll used = min(a[i], now);
        now -= used;
        a[i] -= used;
        ans += used;
        if (a[i]) {
            ll c = (a[i] + k - 1) / k;
            time += c;
            ans += a[i];
            now = c * k - a[i];
        }
        if (i + 1 < n && now < req[i+1]) {
            if (time >= l[i+1]) {
                cout << -1 << endl;
                return 0;
            }
            ans += now;
            now = k;
        }
        if (i + 1 < n) time = l[i+1];
    }
    cout << ans << endl;
}


#include<bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rrep(i,n) for (int i = (n)-1; i >= 0; --i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;

int main() {
    ll n, k;
    cin >> n >> k;
    vector<ll> l(n), r(n), a(n);
    rep(i, n) cin >> l[i] >> r[i] >> a[i];
    vector<ll> req(n);
    ll req_now = 0;
    rrep(i, n) {
        req_now += a[i];
        req_now -= (r[i] - l[i]) * k;
        chmax(req_now, 0);
        if(req_now > k) {
            cout << -1 << endl;
            return 0;
        }
        req[i] = req_now;
    }
    ll ans = 0;
    ll now = k;
    ll time = 0;
    rep(i, n) {
        ll used = min(a[i], now);
        now -= used;
        a[i] -= used;
        ans += used;
        if (a[i]) {
            ll c = (a[i] + k - 1) / k;
            time += c;
            ans += a[i];
            now = c * k - a[i];
        }
        if (i + 1 < n && now < req[i+1]) {
            if (time >= l[i+1]) {
                cout << -1 << endl;
                return 0;
            }
            ans += now;
            now = k;
        }
        if (i + 1 < n) time = l[i+1];
    }
    cout << ans << endl;
}


typedef long long ll;

const ll INF64(1e18);
int n,k;
vector<int>l,r,a;

int main(){
    cin>>n>>k;
    l.resize(n);
    r.resize(n);
    a.resize(n);
    for(int i=0;i<n;++i)
        cin>>l[i]>>r[i]>>a[i];
    vector<ll>dp(n+1,INF64);
    dp[0]=0;
    ll ans=INF64;
    for(int i=0;i<n;++i){
        ll rem=k,total=dp[i];
        for(int j=i;j<n;++j){
            ll cntReload=(max(0ll,a[j]-rem)+k-1)/k;
            if(cntReload>r[j]-l[j])break;
            ll newRem=(rem+cntReload*k)-a[j];
            total+=a[j];
            if(j+1<n){
                if(l[j]+cntReload<l[j+1]){
                    dp[j+1]=min(dp[j+1],total+newRem);
                }
            }
            else{
                ans=min(ans,total);
            }
            rem=newRem;
        }
    }
    if(ans>INF64/2)ans=-1;
    cout<<ans<<endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll INF64(1e18);
int n,k;
vector<int>l,r,a;

int main(){
    cin>>n>>k;
    l.resize(n);
    r.resize(n);
    a.resize(n);
    for(int i=0;i<n;++i)
        cin>>l[i]>>r[i]>>a[i];
    vector<ll>dp(n+1,INF64);
    dp[0]=0;
    ll ans=INF64;
    for(int i=0;i<n;++i){
        ll rem=k,total=dp[i];
        for(int j=i;j<n;++j){
            ll cntReload=(max(0ll,a[j]-rem)+k-1)/k;
            if(cntReload>r[j]-l[j])break;
            ll newRem=(rem+cntReload*k)-a[j];
            total+=a[j];
            if(j+1<n){
                if(l[j]+cntReload<l[j+1]){
                    dp[j+1]=min(dp[j+1],total);
                }
            }
            else{
                ans=min(ans,total);
            }
            rem=newRem;
        }
    }
    if(ans>INF64/2)ans=-1;
    cout<<ans<<endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll INF64(1e18);
int n,k;
vector<int>l,r,a;

int main(){
    cin>>n>>k;
    l.resize(n);
    r.resize(n);
    a.resize(n);
    for(int i=0;i<n;++i)
        cin>>l[i]>>r[i]>>a[i];
    vector<ll>dp(n+1,INF64);
    dp[0]=0;
    ll ans=INF64;
    for(int i=0;i<n;++i){
        ll rem=k,total=dp[i];
        for(int j=i;j<n;++j){
            ll cntReload=(max(0ll,a[j]-rem)+k-1)/k;
            if(cntReload>r[j]-l[j])break;
            ll newRem=(rem+cntReload*k)-a[j];
            total+=a[j];
            if(j+1<n){
                if(l[j]+cntReload<l[j+1]){
                    dp[j+1]=min(dp[j+1],total+newRem);
                }
            }
            else{
                ans=min(ans,total);
            }
            rem=newRem;
        }
    }
    if(ans>INF64/2)ans=-1;
    cout<<ans<<endl;
}


#include <bits/stdc++.h>

#define endl '\n'
#define fi first
#define se second
#define MOD(n,k) ( ( ((n) % (k)) + (k) ) % (k))
#define forn(i,n) for (int i = 0; i < (n); i++)
#define forr(i,a,b) for (int i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define pb(x) push_back(x)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;

const int MX = 2005, inf = 2e9;
int n, k, l[MX], r[MX], a[MX], mem[MX][MX];

int dp (int i, int j, int x) {
	if (i == n) return 0;

	int &res = mem[i][j];
	if (res != -1) return res;
	res = inf;

	int dis = r[i] - l[i] + 1;
	int nec = x >= a[i] ? 1 : (a[i] - x + k - 1) / k + 1;
	int ret = x >= a[i] ? x - a[i] : k - (a[i] - x) % k;
	if (ret == k) ret -= k;

	if (nec <= dis) {
		res = min(res, dp(i + 1, j, ret) + a[i]);
		if (nec < dis || (nec == dis && r[i] < l[i + 1]))
			res = min(res, dp(i + 1, i + 1, k) + a[i] + ret);
	}

	return res;
}

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

	cin >> n >> k;
	forn (i, n) cin >> l[i] >> r[i] >> a[i];

	memset(mem, -1, sizeof(mem));
	int res = dp(0, 0, k);

	if (res == inf) cout << -1 << endl;
	else cout << res << endl;

	return 0;
}


#include <bits/stdc++.h>

#define endl '\n'
#define fi first
#define se second
#define MOD(n,k) ( ( ((n) % (k)) + (k) ) % (k))
#define forn(i,n) for (int i = 0; i < (n); i++)
#define forr(i,a,b) for (int i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define pb(x) push_back(x)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;

const int MX = 2005;
const ll inf = 1e18;
int n, k, l[MX], r[MX], a[MX];
ll mem[MX][MX];

ll dp (int i, int j, int x) {
	if (i == n) return 0;

	ll &res = mem[i][j];
	if (res != -1) return res;
	res = inf;

	int dis = r[i] - l[i] + 1;
	int nec = x >= a[i] ? 1 : (a[i] - x + k - 1) / k + 1;
	int ret = x >= a[i] ? x - a[i] : k - (a[i] - x) % k;
	if (ret == k) ret -= k;

	if (nec <= dis) {
		res = min(res, dp(i + 1, j, ret) + a[i]);
		if (nec < dis || (nec == dis && r[i] < l[i + 1]))
			res = min(res, dp(i + 1, i + 1, k) + a[i] + ret);
	}

	return res;
}

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

	cin >> n >> k;
	forn (i, n) cin >> l[i] >> r[i] >> a[i];

	memset(mem, -1, sizeof(mem));
	ll res = dp(0, 0, k);

	if (res == inf) cout << -1 << endl;
	else cout << res << endl;

	return 0;
}


#include <bits/stdc++.h>

#define endl '\n'
#define fi first
#define se second
#define MOD(n,k) ( ( ((n) % (k)) + (k) ) % (k))
#define forn(i,n) for (int i = 0; i < (n); i++)
#define forr(i,a,b) for (int i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define pb(x) push_back(x)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;

const int MX = 2005, inf = 2e9;
int n, k, l[MX], r[MX], a[MX], mem[MX][MX];

int dp (int i, int j, int x) {
	if (i == n) return 0;

	int &res = mem[i][j];
	if (res != -1) return res;
	res = inf;

	int dis = r[i] - l[i] + 1;
	int nec = x >= a[i] ? 1 : (a[i] - x + k - 1) / k + 1;
	int ret = x >= a[i] ? x - a[i] : k - (a[i] - x) % k;
	if (ret == k) ret -= k;

	if (nec <= dis) {
		res = min(res, dp(i + 1, j, ret) + a[i]);
		if (nec < dis || (nec == dis && r[i] < l[i + 1]))
			res = min(res, dp(i + 1, j + 1, k) + a[i] + ret);
	}

	return res;
}

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

	cin >> n >> k;
	forn (i, n) cin >> l[i] >> r[i] >> a[i];

	memset(mem, -1, sizeof(mem));
	int res = dp(0, 0, k);

	if (res == inf) cout << -1 << endl;
	else cout << res << endl;

	return 0;
}


#include<bits/stdc++.h>
#define per(i,n,x) for(int i=n;i>=x;i--)
#define sz(a) int(a.size())
#define rson mid+1,r,p<<1|1
#define pii pair<int,int>
#define lson l,mid,p<<1
#define ll long long
#define pb push_back
#define mp make_pair
#define se second
#define fi first
using namespace std;
const double eps=1e-8;
const int mod=1e9+7;
const int N=2e3+10;
const int inf=1e9;
ll n,k,l[N],r[N],a[N],dp[N];
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 res=a[i];
        if(r[i]==l[i+1])
            res+=dp[i+1];
        if(1ll*k*(r[i]-l[i]+1)<res)
        {
            cout<<-1;
            return 0;
        }
        dp[i]=max(0ll,res-1ll*k*(r[i]-l[i]));
    }
    ll ans=0,res=k;
    for(int i=1;i<=n;i++)
    {
        ans+=a[i];
        if(a[i]<res)
        {
            res-=a[i];
        }
        else
        {
            a[i]-=res;
            res=(k-a[i]%k)%k;
        }
        if(res<dp[i+1])
        {
            ans+=res;
            res=k;
        }
    }
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=2e3+10;
ll n,k,l[N],r[N],a[N],dp[N];
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 res=a[i];
        if(r[i]==l[i+1])
            res+=dp[i+1];
        if(1ll*k*(r[i]-l[i]+1)<res)
        {
            cout<<-1;
            return 0;
        }
        dp[i]=max(0ll,res-1ll*k*(r[i]-l[i]));
    }
    ll ans=0,res=k;
    for(int i=1;i<=n;i++)
    {
        ans+=a[i];
        if(a[i]<res)
        {
            res-=a[i];
        }
        else
        {
            a[i]-=res;
            res=(k-a[i]%k)%k;
        }
        if(res<dp[i+1])
        {
            ans+=res;
            res=k;
        }
    }
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
#define maxn 2005
#define INF 9999999
#define long long ll
using namespace std;
ll l[maxn], r[maxn], a[maxn],b[maxn];
int main() {
    int n, k;
    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 tmp = a[i];
        if(i+1<=n && r[i] == l[i+1]) tmp += b[i+1];
        if((r[i]-l[i]+1)*k<tmp) {
            cout<<-1;
            return 0;
        }

        b[i] = max(0ll, tmp-(r[i]-l[i])*k);
    }

    ll cur = k;
    ll ans = 0;
    for(int i=1; i<=n; i++) {
        if(cur<b[i]) {
            ans += cur;
            cur = k;
        }
        ans += a[i];
        cur = ((cur-a[i])%k+k)%k;
    }
    cout << ans << endl;
}


#include<bits/stdc++.h>
#define ll long long
#define f(i,x,n) for(int i=x;i<=n;i++)
#define fswp(i,n,x) for(int i=n;i>=x;i--)
#define rrn return
using namespace std;
const int N=2e3+10;
ll n,k,l[N],r[N],a[N],dp[N];
int main()
{
    cin>>n>>k;
    f(i,1,n)
        cin>>l[i]>>r[i]>>a[i];
    fswp(i,n,1)
    {
        ll res=a[i];
        if(r[i]==l[i+1])
            res+=dp[i+1];
        if(k*(r[i]-l[i]+1)<res)
        {
            cout<<-1;
            rrn 0;
        }
        dp[i]=max(0ll,res-1ll*k*(r[i]-l[i]));
    }
    ll ans=0,res=k;
    f(i,1,n)
    {
        ans+=a[i];
        if(a[i]<res)
        {
            res-=a[i];
        }
        else
        {
            a[i]-=res;
            res=(k-a[i]%k)%k;
        }
        if(res<dp[i+1])
        {
            ans+=res;
            res=k;
        }
    }
    cout<<ans;
    rrn 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=2e3+10;
ll n,k,l[N],r[N],a[N],dp[N];
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 res=a[i];
        if(r[i]==l[i+1])
            res+=dp[i+1];
        if(1ll*k*(r[i]-l[i]+1)<res)
        {
            cout<<-1ll;
            return 0;
        }
        dp[i]=max(0ll,res-1ll*k*(r[i]-l[i]));
    }
    ll ans=0,res=k;
    for(int i=1;i<=n;i++)
    {
        ans+=a[i];
        if(a[i]<res)
        {
            res-=a[i];
        }
        else
        {
            a[i]-=res;
            res=(k-a[i]%k)%k;
        }
        if(res<dp[i+1])
        {
            ans+=res;
            res=k;
        }
    }
    cout<<ans;
    return 0;
}


#include<iostream>
#define maxn 2005
#define INF 0x3f3f3f3f
#define long long ll
using namespace std;
ll l[maxn], r[maxn], a[maxn],b[maxn];
int main() {
    int n, k;
    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 tmp = a[i];
        if(i+1<=n && r[i] == l[i+1]) tmp += b[i+1];
        if((r[i]-l[i]+1)*k<tmp) {
            cout<<-1;
            return 0;
        }

        b[i] = max(0ll, tmp-(r[i]-l[i])*k);
    }

    ll cur = k;
    ll ans = 0;
    for(int i=1; i<=n; i++) {
        if(cur<b[i]) {
            ans += cur;
            cur = k;
        }
        ans += a[i];
        cur = ((cur-a[i])%k+k)%k;
    }
    cout << ans << endl;
}


#include<bits/stdc++.h>
#define maxn 2005
#define INF 0x3f3f3f3f
#define long long ll
using namespace std;
ll l[maxn], r[maxn], a[maxn],b[maxn];
int main() {
    int n, k;
    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 tmp = a[i];
        if(i+1<=n && r[i] == l[i+1]) tmp += b[i+1];
        if((r[i]-l[i]+1)*k<tmp) {
            cout<<-1;
            return 0;
        }

        b[i] = max(0ll, tmp-(r[i]-l[i])*k);
    }

    ll cur = k;
    ll ans = 0;
    for(int i=1; i<=n; i++) {
        if(cur<b[i]) {
            ans += cur;
            cur = k;
        }
        ans += a[i];
        cur = ((cur-a[i])%k+k)%k;
    }
    cout << ans << endl;
}


/*
  In the name of ALLAH
  Author : Raashid Anwar
*/

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

#define int int64_t
const int M1 =  998244353;
const int M2 =  1000000007;
mt19937 rng((uint64_t)chrono::steady_clock::now().time_since_epoch().count());

void solve() {
  int n, k;
  cin >> n >> k;
  vector <int> l(n + 1, 0), r(n + 1, 0), a(n + 1, 0);
  vector <vector <int>> dp(n + 1, vector <int> (n + 1, -1)), unused(n + 1, vector <int> (n + 1, 0));
  for (int i = 1; i <= n; i++) {
    cin >> l[i] >> r[i] >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    int bullets = 0, extra = 1;
    for (int j = i; j <= n; j++) {
      int reloads = (a[j] - bullets + k - 1) / k;
      if (bullets)
        extra = 0;
      if (reloads - extra > r[j] - l[j])
        break;
      if (reloads - extra < r[j] - l[j])
        extra = 1;
      if (j < n && l[j + 1] > r[j])
        extra = 1;
      dp[i][j] = (j > i? dp[i][j - 1]: 0) + a[j];
      unused[i][j] = bullets - a[j] + ((a[j] - bullets + k - 1) / k) * k;
      bullets = unused[i][j];
    }
  }
  vector <int> ok(n + 1, false);
  vector <int> ans(n + 1, M2 * M2);
  ok[0] = true;
  ans[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = i; j >= 1; j--) {
      if (dp[j][i] != -1)
      if (ok[j - 1] && dp[j][i] != -1) {
        ans[i] = min(ans[i], ans[j - 1] + dp[j][i] + (i != n? unused[j][i]: 0));
        ok[i] = true;
      }
    }
  }
  if (!ok[n]) {
    cout << -1 << "\n";
  } else {
    cout << ans[n] << "\n";
  }
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
}


/*
  In the name of ALLAH
  Author : Raashid Anwar
*/

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

#define int int64_t
const int M1 =  998244353;
const int M2 =  1000000007;
mt19937 rng((uint64_t)chrono::steady_clock::now().time_since_epoch().count());

void solve() {
  int n, k, sum = 0;
  cin >> n >> k;
  vector <int> l(n + 1, 0), r(n + 1, 0), a(n + 1, 0);
  vector <vector <int>> dp(n + 1, vector <int> (n + 1, -1));
  for (int i = 1; i <= n; i++) {
    cin >> l[i] >> r[i] >> a[i];
    sum += a[i];
  }
  for (int i = 1; i <= n; i++) {
    int bullets = k, reloads = 0;
    for (int j = i; j <= n; j++) {
      if (a[j] <= bullets) {
        reloads = 0;
        bullets = bullets - a[j];
      } else if ((a[j] - bullets) % k == 0) {
        reloads = (a[j] - bullets) / k;
        bullets = 0;
      } else {
        reloads = (a[j] - bullets) / k + 1;
        bullets = k - (a[j] - bullets) % k;
      }
      if (reloads > r[j] - l[j])
        break;
      if (j == n || reloads < r[j] - l[j] || l[j + 1] > r[j])
        dp[i][j] = (j != n? bullets: 0);
    }
  }
  vector <int> ok(n + 1, false);
  vector <int> ans(n + 1, M2 * M2);
  ok[0] = true;
  ans[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = i; j >= 1; j--) {
      if (ok[j - 1] && dp[j][i] != -1) {
        ans[i] = min(ans[i], ans[j - 1] + dp[j][i]);
        ok[i] = true;
      }
    }
  }
  if (!ok[n]) {
    cout << -1 << "\n";
  } else {
    cout << sum + ans[n] << "\n";
  }
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
}


/*
  In the name of ALLAH
  Author : Raashid Anwar
*/

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

#define int int64_t
const int M1 =  998244353;
const int M2 =  1000000007;
mt19937 rng((uint64_t)chrono::steady_clock::now().time_since_epoch().count());

void solve() {
  int n, k;
  cin >> n >> k;
  vector <int> l(n + 1, 0), r(n + 1, 0), a(n + 1, 0);
  vector <vector <int>> dp(n + 1, vector <int> (n + 1, -1)), unused(n + 1, vector <int> (n + 1, 0));
  for (int i = 1; i <= n; i++) {
    cin >> l[i] >> r[i] >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    int bullets = 0;
    for (int j = i; j <= n; j++) {
      int reloads = (a[j] - bullets + k - 1) / k, extra = 0;
      if (!bullets && l[j] > r[j - 1])
          extra = 1;
      if (reloads - extra > r[j] - l[j])
        break;
      dp[i][j] = (j > i? dp[i][j - 1]: 0) + a[j];
      unused[i][j] = bullets - a[j] + ((a[j] - bullets + k - 1) / k) * k;
      bullets = unused[i][j];
    }
  }
  vector <int> ok(n + 1, false);
  vector <int> ans(n + 1, M2 * M2);
  ok[0] = true;
  ans[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = i; j >= 1; j--) {
      if (ok[j - 1] && dp[j][i] != -1) {
        ans[i] = min(ans[i], ans[j - 1] + dp[j][i] + (i != n? unused[j][i]: 0));
        ok[i] = true;
      }
    }
  }
  if (!ok[n]) {
    cout << -1 << "\n";
  } else {
    cout << ans[n] << "\n";
  }
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fast_IO ios::sync_with_stdio(false);
#define DEBUG fprintf(stderr,"Running on Line %d in Function %s\n",__LINE__,__FUNCTION__)
//mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define fir first
#define sec second
#define mod 998244353
#define int long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
inline int read()
{
	char ch=getchar(); int nega=1; while(!isdigit(ch)) {if(ch=='-') nega=-1; ch=getchar();}
	int ans=0; while(isdigit(ch)) {ans=ans*10+ch-48;ch=getchar();}
	if(nega==-1) return -ans;
	return ans;
}
typedef pair<int,int> pii;
#define N 2005
int n,k,a[N];
int l[N],r[N],v[N];
vector<pii> f,g;
signed main()
{
	cin>>n>>k;
	for(int i=1;i<=n;i++)
	{
		l[i]=read(),r[i]=read(),v[i]=read();
		if(l[i]==r[i])
		{
			if(l[i]==r[i-1]) {v[i-1]+=v[i],i--,n--; continue;}
		}
		if(i!=1&&l[i-1]==r[i-1])
		{
			if(l[i]==r[i-1]) {l[i-1]=l[i],r[i-1]=r[i],v[i-1]+=v[i],i--,n--; continue;}
		}
	}
	f.eb(k,0);
	for(int i=1;i<=n;i++)
	{
		if(l[i]>r[i-1])
		{
			int minn=INF;
			for(auto [x,y]:f)
			{
				minn=min(minn,(x==k?0:x)+y);
				if(x!=k) g.eb(x,y);
			}
			if(minn<INF) g.eb(k,minn);
			f=g; g.clear();
		}
		for(auto [x,y]:f)
		{
			int A=y+v[i];
			int ned=(max(0LL,v[i]-x)+k-1)/k;
			if(l[i]+ned>r[i]) continue;
			int B=ned*k-v[i]+x;
			g.eb(B,A);
			if(l[i]+ned!=r[i]) g.eb(k,A+B);
		}
		f=g; g.clear();
	}
	int ans=INF;
	for(auto [x,y]:f) ans=min(ans,y);
	if(ans==INF) cout<<-1<<endl;
	else cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fast_IO ios::sync_with_stdio(false);
#define DEBUG fprintf(stderr,"Running on Line %d in Function %s\n",__LINE__,__FUNCTION__)
//mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define fir first
#define sec second
#define mod 998244353
#define int long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
inline int read()
{
	char ch=getchar(); int nega=1; while(!isdigit(ch)) {if(ch=='-') nega=-1; ch=getchar();}
	int ans=0; while(isdigit(ch)) {ans=ans*10+ch-48;ch=getchar();}
	if(nega==-1) return -ans;
	return ans;
}
typedef pair<int,int> pii;
#define N 2005
int n,k,a[N];
int f[N],l[N],r[N],v[N];
signed main()
{
	cin>>n>>k;
	for(int i=1;i<=n;i++)
	{
		l[i]=read(),r[i]=read(),v[i]=read();
		if(l[i]==r[i])
		{
			if(l[i]==r[i-1]) {v[i-1]+=v[i],i--,n--; continue;}
		}
		if(i!=1&&l[i-1]==r[i-1])
		{
			if(l[i]==r[i-1]) {l[i-1]=l[i],r[i-1]=r[i],v[i-1]+=v[i],i--,n--; continue;}
		}
	}
	for(int i=1;i<=n;i++) f[i]=INF;
	f[0]=k;
	int ans=INF;
	for(int i=1;i<=n;i++)
	{
		int cnt=0,cur=k;
		for(int j=i;j<=n;j++)
		{
			int tgo=0,tim=0;
			bool ok=1;
			// printf("%d %d %d\n",i,j,cur);
			if(r[j]!=l[j+1])
			{
				if((r[j]-l[j])*k+cur>=v[j])
				{
					int go=(v[j]-cur+k)/k;
					// printf("%d %d %d\n",i,j,go);
					cnt+=go;
					cur=cur+go*k-v[j];
					f[j]=min(f[j],f[i-1]+cnt*k+(cur!=k)*k);
					if(j==n) ans=min(ans,f[i-1]+cnt*k-cur);
				}
				else break;
				continue;
			}
			if((r[j]-l[j]-1)*k+cur>=v[j])
			{
				int go=(v[j]-cur+k)/k;
				cnt+=go;
				cur=cur+go*k-v[j];
				f[j]=min(f[j],f[i-1]+cnt*k+(cur!=k)*k);
				if(j==n) ans=min(ans,f[i-1]+cnt*k-cur);
			}
			else if((r[j]-l[j])*k+cur>=v[j])
			{
				int go=r[j]-l[j];
				cnt+=go;
				cur=cur+go*k-v[j];
				tim=1,tgo=cur,cur=0;
			}
			else break;
			// printf("%d %d %d %d\n",i,j,tim,tgo);
			while(j!=n&&r[j]==l[j+1])
			{
				j++;
				if((r[j]-l[j]-tim)*k+cur>=v[j]-tgo)
				{
					int go=(v[j]-tgo-cur+k)/k;
					cnt+=go;
					cur=cur+go*k-v[j];
					f[j]=min(f[j],f[i-1]+cnt*k+(cur!=k)*k);
					tgo=0,tim=0;
					if(j==n) ans=min(ans,f[i-1]+cnt*k-cur);
				}
				else if((r[j]-l[j]-tim+1)*k+cur>=v[j]-tgo)
				{
					int go=r[j]-l[j]-tim+1;
					cnt+=go;
					cur=cur+go*k-v[j];
					tim=1,tgo=cur,cur=0;
				}
				else {ok=0; break;}
			}
			if(!ok) break;
			if(j==n) ans=min(ans,f[i-1]+cnt*k-cur);
		}
	}
	if(ans==INF) cout<<-1<<endl;
	else cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fast_IO ios::sync_with_stdio(false);
#define DEBUG fprintf(stderr,"Running on Line %d in Function %s\n",__LINE__,__FUNCTION__)
//mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define fir first
#define sec second
#define mod 998244353
#define int long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
inline int read()
{
	char ch=getchar(); int nega=1; while(!isdigit(ch)) {if(ch=='-') nega=-1; ch=getchar();}
	int ans=0; while(isdigit(ch)) {ans=ans*10+ch-48;ch=getchar();}
	if(nega==-1) return -ans;
	return ans;
}
typedef pair<int,int> pii;
#define N 2005
int n,k,a[N];
int l[N],r[N],v[N];
vector<pii> f,g;
signed main()
{
	cin>>n>>k;
	for(int i=1;i<=n;i++) l[i]=read(),r[i]=read(),v[i]=read();
	f.eb(k,0);
	for(int i=1;i<=n;i++)
	{
		if(l[i]>r[i-1])
		{
			int minn=INF;
			for(auto [x,y]:f)
			{
				minn=min(minn,(x==k?0:x)+y);
				if(x!=k) g.eb(x,y);
			}
			if(minn<INF) g.eb(k,minn);
			f=g; g.clear();
		}
		for(auto [x,y]:f)
		{
			int A=y+v[i];
			int ned=(max(0LL,v[i]-x)+k-1)/k;
			if(l[i]+ned>r[i]) continue;
			int B=ned*k-v[i]+x;
			g.eb(B,A);
			if(l[i]+ned!=r[i]) g.eb(k,A+B);
		}
		f=g; g.clear();
	}
	int ans=INF;
	for(auto [x,y]:f) ans=min(ans,y);
	if(ans==INF) cout<<-1<<endl;
	else cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    ll k;
    cin >> n >> k;
    vector<array<ll,3>> v;
    array<ll,3> tmp;
    for(int i=0;i<n;i++)
    {
        ll l,r,a;
        cin >> l >> r >> a;
        if(i==0) tmp={l,r,a};
        else if(tmp[1]==l) tmp={tmp[0],r,tmp[2]+a};
        else
        {
            v.push_back(tmp);
            tmp={l,r,a};
        }
    }
    v.push_back(tmp);
    n=v.size();
    vector<ll> req(n,0);
    for(int i=0;i<n;i++)
    {
        req[i]=max(0ll,v[i][2]-(v[i][1]-v[i][0])*k);
        if(req[i]>k)
        {
            cout << "-1\n";
            return 0;
        }
    }
    vector<ll> dp(n,(1ll<<60));
    ll sum=0;
    for(int i=n-1;i>=0;i--)
    {
        sum+=v[i][2];
        ll now=k;
        bool ok=1;
        for(int j=i;j<n;j++)
        {
            dp[i]=min(dp[i],now+dp[j]);
            if(now<req[j])
            {
                ok=0;
                break;
            }
            now=((now-v[j][2])%k+k)%k;
            if(now==0) now=k;
        }
        if(ok) dp[i]=0;
    }
    cout << sum+dp[0] << "\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    ll k;
    cin >> n >> k;
    vector<ll> l(n);
    vector<ll> r(n);
    vector<ll> a(n);
    vector<ll> req(n);
    for(int i=0;i<n;i++)
    {
        cin >> l[i] >> r[i] >> a[i];
        req[i]=max(0ll,a[i]-(r[i]-l[i])*k);
    }
    const ll inf=(1ll<<60);
    vector<ll> dp(n,inf);
    ll sum=0;
    for(int i=n-1;i>=0;i--)
    {
        sum+=a[i];
        ll now=k;
        bool ok=1;
        for(int j=i;j<n;j++)
        {
            if(i!=j&&r[j-1]!=l[j]) dp[i]=min(dp[i],now+dp[j]);
            if(now<req[j])
            {
                ok=0;
                break;
            }
            if(j<n-1&&r[j]==l[j+1]&&l[j]<r[j]&&now+(r[j]-l[j]-1)*k>=a[j]) dp[i]=min(dp[i],((now-a[j])%k+k)%k+dp[j+1]);
            now=((now-a[j])%k+k)%k;
        }
        if(ok) dp[i]=0;
    }
    if(dp[0]!=inf) cout << sum+dp[0] << "\n";
    else cout << "-1\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    ll k;
    cin >> n >> k;
    vector<ll> v;
    vector<ll> req;
    vector<array<ll,3>> tmp;
    auto process=[&]()
    {
        ll sum=0;
        ll x=0;
        for(int j=0;j<(int)tmp.size();j++)
        {
            sum+=tmp[j][2];
            x=max(x,sum-(tmp[j][1]-tmp[0][0])*k);
        }
        if(x>k)
        {
            cout << "-1\n";
            exit(0);
        }
        v.push_back(sum);
        req.push_back(x);
        tmp.clear();
    };
    for(int i=0;i<n;i++)
    {
        ll l,r,a;
        cin >> l >> r >> a;
        if(tmp.empty()||tmp.back()[1]==l) tmp.push_back({l,r,a});
        else
        {
            process();
            tmp.push_back({l,r,a});
        }
    }
    process();
    n=v.size();
    vector<ll> dp(n,(1ll<<60));
    ll sum=0;
    for(int i=n-1;i>=0;i--)
    {
        sum+=v[i];
        ll now=k;
        bool ok=1;
        for(int j=i;j<n;j++)
        {
            dp[i]=min(dp[i],now+dp[j]);
            if(now<req[j])
            {
                ok=0;
                break;
            }
            now=((now-v[j])%k+k)%k;
            if(now==0) now=k;
        }
        if(ok) dp[i]=0;
    }
    cout << sum+dp[0] << "\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    ll k;
    cin >> n >> k;
    vector<ll> l(n);
    vector<ll> r(n);
    vector<ll> a(n);
    vector<ll> req(n);
    for(int i=0;i<n;i++)
    {
        cin >> l[i] >> r[i] >> a[i];
        req[i]=max(0ll,a[i]-(r[i]-l[i])*k);
    }
    const ll inf=(1ll<<60);
    vector<ll> dp(n,inf);
    ll sum=0;
    for(int i=n-1;i>=0;i--)
    {
        sum+=a[i];
        ll now=k;
        bool ok=1;
        //cout << "go from i=" << i << " refilling before i" << endl;
        for(int j=i;j<n;j++)
        {
            if(i!=j&&r[j-1]!=l[j]) dp[i]=min(dp[i],now+dp[j]);
            if(now<req[j])
            {
                //cout << "not possible, now=" << now << " < req=" << req[j] << endl;
                ok=0;
                break;
            }
            //cout << "in " << j << " with now=" << now << endl;
            if(j<n-1&&r[j]==l[j+1]&&l[j]<r[j]&&now+(r[j]-l[j]-1)*k>=a[j]) dp[i]=min(dp[i],((now-a[j])%k+k)%k+dp[j+1]);
            now=((now-a[j])%k+k)%k;
            //if(now==0) now=k;
        }
        if(ok) dp[i]=0;
    }
    if(dp[0]!=inf) cout << sum+dp[0] << "\n";
    else cout << "-1\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    ll k;
    cin >> n >> k;
    vector<array<ll,3>> v;
    array<ll,3> tmp;
    for(int i=0;i<n;i++)
    {
        ll l,r,a;
        cin >> l >> r >> a;
        if(i==0) tmp={l,r,a};
        else if(tmp[1]==l) tmp={tmp[0],r,tmp[2]+a};
        else
        {
            v.push_back(tmp);
            tmp={l,r,a};
        }
    }
    v.push_back(tmp);
    n=v.size();
    vector<ll> req(n,0);
    for(int i=0;i<n;i++)
    {
        req[i]=max(0ll,v[i][2]-(v[i][1]-v[i][0])*k);
        if(req[i]>k)
        {
            cout << "-1\n";
            return 0;
        }
    }
    ll now=k;
    ll res=0;
    for(int i=0;i<n;i++)
    {
        auto [l,r,a]=v[i];
        if(now<req[i])
        {
            res+=now;
            now=k;
        }
        ll reload=(a-now+k-1)/k;
        res+=a;
        now=(now+k*reload-a);
    }
    cout << res << "\n";
    return 0;
}


#include<bits/stdc++.h>
#define endl '\n'
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define lg2(x) __lg(x)
#define rem_dupl(x) sort(all(x)); x.erase(unique(all(x)), x.end())
using namespace std;

typedef long long ll;
typedef long double db;
typedef pair<ll,ll> ii;
#define x first
#define y second

const int maxn = 2005;
ll l[maxn], r[maxn], a[maxn];
ll sa[maxn];

vector<ii> dp[maxn]; /// dp[n][k]: min cost of ending at the end of the n-th wave with k bullets left

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cout.setf(ios::fixed); cout.precision(0);

    int n, k;
    cin >> n >> k;
    for(int i = 1; i <= n; i++)
        cin >> l[i] >> r[i] >> a[i];

    dp[0] = {{k, 0}};
    for(int i = 0; i < n; i++){
        for(auto [j, val]:dp[i]){

            int x = a[i+1] % k;
            if(x == 0)
                x = k;

            ll reloads = (a[i+1] + k - 1)/k;

            if(j >= x){
                j -= x;
                x = 0;
                reloads--;

                if(reloads > r[i+1] - l[i+1])
                    continue;

                dp[i+1].push_back({j, val});

                if(reloads < r[i+1] - l[i+1] or l[i+2] > r[i+1])
                    dp[i+1].push_back({k, val + j});

                continue;

            }
            if(j < x) {

                x -= j;
                j = 0;

                if(reloads > r[i+1] - l[i+1])
                    continue;

                dp[i+1].push_back({k - x, val});

                if(reloads < r[i+1] - l[i+1] or l[i+2] > r[i+1] )
                    dp[i+1].push_back({k, val + k - x});
            }
        }
        if(!dp[i+1].empty()){
            sort(all(dp[i+1]));
            vector<ii> aux;
            for(int j = 0; j < dp[i+1].size(); j++)
                if(j == 0 or dp[i+1][j].x != dp[i+1][j-1].x)
                    aux.push_back(dp[i+1][j]);
            dp[i+1] = aux;
        }

        dp[i].clear();
        dp[i].shrink_to_fit();
    }

    if(dp[n].empty()) cout << -1;
    else{
        ll ans = LLONG_MAX;
        for(auto i:dp[n])
            ans = min(ans, i.y);
        for(int i = 1; i <= n; i++)
            ans += a[i];
        cout << ans;
    }

    return 0;
}


#include<bits/stdc++.h>
#define endl '\n'
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define lg2(x) __lg(x)
#define rem_dupl(x) sort(all(x)); x.erase(unique(all(x)), x.end())
using namespace std;

typedef long long ll;
typedef long double db;
typedef pair<ll,ll> ii;
#define x first
#define y second

const int maxn = 2005;
ll l[maxn], r[maxn], a[maxn];
ll sa[maxn];

vector<ii> dp[maxn]; /// dp[n][k]: min cost of ending at the end of the n-th wave with k bullets left

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cout.setf(ios::fixed); cout.precision(0);

    int n, k;
    cin >> n >> k;
    for(int i = 1; i <= n; i++)
        cin >> l[i] >> r[i] >> a[i];

    l[n+1] = r[n] + 1;
    r[n+1] = l[n+1];
    a[n+1] = 0;

    for(int i = n; i >= 0; i--)
        sa[i] = sa[i+1] + a[i];

    dp[0] = {{k, 0}};
    for(int i = 0; i < n; i++){
        for(auto [j, val]:dp[i]){

            int x = a[i+1] % k;
            if(x == 0)
                x = k;

            ll reloads = (a[i+1] + k - 1)/k;

            if(j >= x){
                j -= x;
                x = 0;
                reloads--;

                if(reloads > r[i+1] - l[i+1])
                    continue;

                dp[i+1].push_back({j, val});

                if(reloads < r[i+1] - l[i+1] or (i+2 <= n and l[i+2] > r[i+1]))
                    dp[i+1].push_back({k, val + j});

                continue;

            }
            if(j < x) {

                x -= j;
                j = 0;

                if(reloads > r[i+1] - l[i+1])
                    continue;

                dp[i+1].push_back({k - x, val});

                if(reloads < r[i+1] - l[i+1] or (i+2 <= n and l[i+2] > r[i+1]) )
                    dp[i+1].push_back({k, val + k - x});
            }
        }
        rem_dupl(dp[i+1]);
    }

    if(dp[n].empty()) cout << -1;
    else{
        ll ans = LLONG_MAX;
        for(auto i:dp[n])
            ans = min(ans, i.y);
        for(int i = 1; i <= n; i++)
            ans += a[i];
        cout << ans;
    }

    return 0;
}