#include <bits/stdc++.h>
#define clog(x) std::clog << (#x) << " is " << (x) << '\n';
using LL = long long;

const int N = 2002;
struct Edge {
	int to;
	LL c, d;
	Edge(int x, LL y, LL z) : to(x), c(y), d(z) {}
};
const LL INF = 1e18;
int main() {
	//freopen("in", "r", stdin);
	std::cin.tie(nullptr)->sync_with_stdio(false);
	int cas = 1;
	// std::cin >> cas;
	while (cas--) {
		int n, m;
		std::cin >> n >> m;
		std::vector<std::vector<Edge>> e(n);
		for (int i = 0, u, v; i < m; ++i) {
			LL c, d;
			std::cin >> u >> v >> c >> d;
			if (--u == --v) continue;
			e[u].emplace_back(v, c, d);
			e[v].emplace_back(u, c, d);
		}
		auto f = [&](LL c, LL d, LL t) -> LL {
			int sn = std::sqrt(d);
			if (t > sn) return c + t + d / (t + 1);
			return c + sn + std::min(d / (sn + 1), d / (sn + 2) + 1);
		};
		std::vector<LL> dist(n, INF);
		dist[0] = 0;
		std::priority_queue<std::pair<LL, int>> Q;
		Q.push({0, 0});
		while (!Q.empty()) {
			auto [du, u] = Q.top();
			Q.pop();
			if (du != -dist[u]) continue;
			for (auto [v, c, d] : e[u]) {
				LL now = f(c, d, -du);
				if (dist[v] > now) {
					dist[v] = now;
					Q.push({-now, v});
				}
			}
		}
		std::cout << (dist[n - 1] == INF ? -1 : dist[n - 1]) << '\n';
	}
	return 0;
}
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

template <class T>
using idx_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define ull unsigned long long
#define arr array
#define vt vector
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vc vector<char>
#define vvc vector<vector<char>>
#define all(v) (v).begin(), (v).end()
#define pi acosl(-1)

//ofstream out("debug.txt");

const int MxN = 1e5, md = 1e9 + 7;

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

	ll n, C;
	cin >> n >> C;
	vt<arr<ll, 2>> v;
	ll a, b, c;
	for (int i = 0; i < n; ++i)
		cin >> a >> b >> c, v.push_back({a, c}), v.push_back({b + 1, -c});
	sort(all(v));
	ll cf = 0, tf = 0, ld = 0;
	for (auto a : v)
	{
		tf += min(cf, C) * (a[0] - ld);
		cf += a[1];
		ld = a[0];
	}
	cout << tf << '\n';
}

