This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// BEGIN BOILERPLATE CODE
#include <bits/stdc++.h>
using namespace std;
#ifdef KAMIRULEZ
	const bool kami_loc = true;
	const long long kami_seed = 69420;
#else
	const bool kami_loc = false;
	const long long kami_seed = chrono::steady_clock::now().time_since_epoch().count();
#endif
const string kami_fi = "kamirulez.inp";
const string kami_fo = "kamirulez.out";
mt19937_64 kami_gen(kami_seed);
long long rand_range(long long rmin, long long rmax) {
	uniform_int_distribution<long long> rdist(rmin, rmax);
	return rdist(kami_gen);
}
void file_io(string fi, string fo) {
	if (fi != "" && (!kami_loc || fi == kami_fi)) {
		freopen(fi.c_str(), "r", stdin);
	}
	if (fo != "" && (!kami_loc || fo == kami_fo)) {
		freopen(fo.c_str(), "w", stdout);
	}
}
void set_up() {
	if (kami_loc) {
		file_io(kami_fi, kami_fo);
	}
	ios_base::sync_with_stdio(0);
	cin.tie(0);
}
void just_do_it();
void just_exec_it() {
	if (kami_loc) {
		auto pstart = chrono::steady_clock::now();
		just_do_it();
		auto pend = chrono::steady_clock::now();
		long long ptime = chrono::duration_cast<chrono::milliseconds>(pend - pstart).count();
		string bar(50, '=');
		cout << '\n' << bar << '\n';
		cout << "Time: " << ptime << " ms" << '\n';
	}
	else {
		just_do_it();
	}
}
int main() {
	set_up();
	just_exec_it();
	return 0;
}
// END BOILERPLATE CODE
// BEGIN MAIN CODE
const long long inf = (long long)1e18 + 20;
struct node {
	int lt = -1;
	int rt = -1;
	long long ts = inf;
	node *lx = nullptr;
	node *rx = nullptr;
};
void update(node *cc, int px, long long pp) {
	int lt = cc->lt;
	int rt = cc->rt;
	if (lt == rt) {
		cc->ts = min(cc->ts, pp);
		return;
	}
	int mt = (lt + rt) / 2;
	if (px <= mt) {
		if (!cc->lx) {
			cc->lx = new node({lt, mt, inf, nullptr, nullptr});
		}
		update(cc->lx, px, pp);
	}
	else {
		if (!cc->rx) {
			cc->rx = new node({mt + 1, rt, inf, nullptr, nullptr});
		}
		update(cc->rx, px, pp);
	}
	cc->ts = min((cc->lx ? cc->lx->ts : inf), (cc->rx ? cc->rx->ts : inf));
}
long long get(node *cc, int ql, int qr) {
	if (!cc) {
		return inf;
	}
	int lt = cc->lt;
	int rt = cc->rt;
	if (lt == ql && rt == qr) {
		return cc->ts;
	}
	int mt = (lt + rt) / 2;
	if (qr <= mt) {
		return get(cc->lx, ql, qr);
	}
	else if (ql >= mt + 1) {
		return get(cc->rx, ql, qr);
	}
	else {
		return min(get(cc->lx, ql, mt), get(cc->rx, mt + 1, qr));
	}
}
node *root[2];
void just_do_it() {
	int m, n;
	cin >> m >> n;
	root[0] = new node({1, n, inf, nullptr, nullptr});
	root[1] = new node({1, n, inf, nullptr, nullptr});
	update(root[0], 1, 0);
	update(root[1], n, 0);
	long long res = inf;
	for (int i = 0; i < m; i++) {
		int a, b, c, d;
		cin >> a >> b >> c >> d;
		long long gl = get(root[0], a, b);
		long long gr = get(root[1], a, b);
		res = min(res, gl + gr + d);
		update(root[0], c, gl + d);
		update(root[1], c, gr + d);
	}
	if (res == inf) {
		res = -1;
	}
	cout << res;
}
// END MAIN CODE
Compilation message (stderr)
pinball.cpp: In function 'void file_io(std::string, std::string)':
pinball.cpp:25:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |   freopen(fi.c_str(), "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
pinball.cpp:28:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |   freopen(fo.c_str(), "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |