#include <algorithm>
#include <iostream>
#include <map>
#include <set>

using namespace std;

const int N = 200000;

int aa[N << 1], bb[N], ii[N << 1];
long long dd[N << 1];

int main() {
	int n; cin >> n;
	long long b_; cin >> b_;
	for (int i = 0; i < n; i++)
		cin >> aa[i << 1 | 0] >> aa[i << 1 | 1] >> bb[i], aa[i << 1 | 1]++;
	for (int h = 0; h < n << 1; h++)
		ii[h] = h;
	sort(ii, ii + (n << 1), [] (int i, int j) { return aa[i] < aa[j]; });
	map<int, int> mp;
	int a_ = 0;
	for (int h = 0; h < n << 1; h++) {
		int a = aa[ii[h]];
		mp[a] = h + 1 == n << 1 || aa[ii[h + 1]] != a ? a_++ : a_;
	}
	for (int i = 0; i < n; i++)
		dd[mp[aa[i << 1 | 0]]] += bb[i], dd[mp[aa[i << 1 | 1]]] -= bb[i];
	for (int a = 1; a < a_; a++)
		dd[a] += dd[a - 1];
	set<int> s;
	for (int i = 0; i < n << 1; i++)
		s.insert(aa[i]);
	long long ans = 0;
	while (s.size() >= 2) {
		int a = *s.begin(); s.erase(s.begin());
		int b = *s.begin();
		ans += min(dd[mp[a]], b_) * (b - a);
	}
	ans += min(dd[mp[*s.begin()]], b_);
	cout << ans << '\n';
	return 0;
}

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define x first
#define y second

using namespace std;

typedef long long ll;

int a[200005], b[200005];
pair <int, int> masA[200005], masB[200005];
int perm[200005], fen[200005];

int f(int x)
{
 	return (x & (-x));
}

int get_sum(int pos, int n)
{
 	int s = 0;
 	for (int i = pos; i <= n; i += f(i)) s += fen[i];
 	return s;
}

void update(int pos, int n)
{
 	for (int i = pos; i > 0; i -= f(i)) fen[i]++;
 	return;
}

int main()
{
 	//freopen("input.txt", "r", stdin);
 	//freopen("output.txt", "w", stdout);
 	ios_base::sync_with_stdio(0); cin.tie(0);
 	int n;
 	cin >> n;
 	for (int i = 1; i <= n; i++)
 	{
 		cin >> a[i];
 		masA[i] = mp(a[i] + i, i);
   	}
 	for (int i = 1; i <= n; i++)
 	{
 		cin >> b[i];
 		masB[i] = mp(b[i] + i, i);
 	}
	sort(masA + 1, masA + n + 1);
	sort(masB + 1, masB + n + 1);

	bool fl = true;
	for (int i = 1; i <= n; i++)
		if (masA[i].x != masB[i].x) fl = false;

	if (!fl) cout << "-1\n", exit(0);

	for (int i = 1; i <= n; i++) perm[masA[i].y] = masB[i].y;
	ll ans = 0;
	for (int i = 1; i <= n; i++)
	{
	 	ans += get_sum(perm[i], n);
	 	update(perm[i], n);
	}
	cout << ans << '\n';
 	return 0;
}