Problem: Note that the differences between easy and hard versions are the constraints on n and the time limit. You can make hacks only if both versions are solved.

AquaMoon knew through foresight that some ghosts wanted to curse tourists on a pedestrian street. But unfortunately, this time, these ghosts were hiding in a barrier, and she couldn't enter this barrier in a short time and destroy them. Therefore, all that can be done is to save any unfortunate person on the street from the ghosts.

The pedestrian street can be represented as a one-dimensional coordinate system. There is one person hanging out on the pedestrian street. At the time 0 he is at coordinate x, moving with a speed of 1 unit per second. In particular, at time i the person will be at coordinate x+i.

The ghosts are going to cast n curses on the street. The i-th curse will last from time tl_i-1+10^{-18} to time tr_i+1-10^{-18} (exclusively) and will kill people with coordinates from l_i-1+10^{-18} to r_i+1-10^{-18} (exclusively). Formally that means, that the person, whose coordinate is between (l_i-1+10^{-18},r_i+1-10^{-18}) in the time range (tl_i-1+10^{-18},tr_i+1-10^{-18}) will die.

To save the person on the street, AquaMoon can stop time at any moment t, and then move the person from his current coordinate x to any coordinate y (t, x and y are not necessarily integers). The movement costs AquaMoon |x-y| energy. The movement is continuous, so if there exists some cursed area between points x and y at time t, the person will die too.

AquaMoon wants to know what is the minimum amount of energy she needs to spend in order to save the person on the street from all n curses. But she is not good at programming. As her friend, can you help her?

Input

The first line contains a single integer n (1≤ n≤ 2000) — the number of curses.

The next line contains a single integer x (1≤ x≤ 10^6) — the initial coordinate of the person.

The following n lines contain four integers tl_i, tr_i, l_i, r_i each (1≤ tl_i≤ tr_i≤ 10^6, 1≤ l_i≤ r_i≤ 10^6).

Output

Print a single integer — the minimum energy which AquaMoon needs to spent, rounded up to the nearest integer (in case there are two nearest integers you should round the answer to the highest of them).

Examples

Input


2
1
1 2 1 2
2 3 2 3


Output


2

Input


3
4
1 4 1 2
1 4 4 15
6 7 1 4


Output


8

Input


4
3
1 5 1 1
4 10 1 4
1 2 3 13
1 10 7 19


Output


14

Input


7
5
78 96 76 91
6 16 18 37
53 63 40 56
83 88 21 38
72 75 17 24
63 63 53 60
34 46 60 60


Output


20

Solution (in CPP):

Solution:
#include <algorithm>  
#include <iostream>  
#include <sstream>  
#include <string>  
#include <vector>  
#include <queue>  
#include <set>  
#include <map>  
#include <cstdio>  
#include <cstdlib>  
#include <cctype>  
#include <cmath>  
#include <cstring>
#include <list>  
#include <cassert>
#include <climits>
#include <bitset>
#include <chrono>
#include <random>
using namespace std;

#define PB push_back  
#define MP make_pair  
#define SZ(v) ((int)(v).size())  
#define FOR(i,a,b) for(int i=(a);i<(b);++i)  
#define REP(i,n) FOR(i,0,n)  
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)  
#define REPE(i,n) FORE(i,0,n)  
#define FORSZ(i,a,v) FOR(i,a,SZ(v))  
#define REPSZ(i,v) REP(i,SZ(v))  
std::mt19937 rnd((int)std::chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

const int MAXRECT = 2000;
const int INF = 1000000000;
struct Rect { int lt, rt, lx, rx; };
struct Line { 
	int ly, ry, lcost, delta;
	Line() {} 
	Line(int ly, int ry, int lcost, int delta) :ly(ly), ry(ry), lcost(lcost), delta(delta) {}
	int rcost() const { return lcost + (ry - ly) * delta; }
};
typedef vector<Line> Seg;


int nrect, sx;
Rect rect[MAXRECT];

vector<Seg> segs;
vector<int> e;

void ltrim(Seg& s, int ny) {
	while (SZ(s) >= 2 && ny >= s[0].ry) s.erase(s.begin());
	if (s[0].ry == ny) {
		assert(SZ(s) == 1);
		s[0].lcost = s[0].rcost(), s[0].ly = s[0].ry, s[0].delta = 0;
	} else {
		s[0].lcost += (ny - s[0].ly) * s[0].delta;
		s[0].ly = ny;
	}
}
void rtrim(Seg& s, int ny) {
	while (SZ(s) >= 2 && ny <= s.back().ly) s.pop_back();
	if (s.back().ly == ny) {
		assert(SZ(s) == 1);
		s[0].ry = s[0].ly, s[0].delta = 0;
	} else {
		s.back().ry = ny;
	}
}
void lextend(Seg& s, int ny) {
	if (s[0].ly == ny) return;
	if (s[0].delta == -1 || s[0].ly == s[0].ry) {
		s[0].lcost += s[0].ly - ny;
		s[0].ly = ny;
		s[0].delta = -1;
	} else {
		s.insert(s.begin(), Line(ny, s[0].ly, s[0].lcost + s[0].ly - ny, -1));
	}
}
void rextend(Seg& s, int ny) {
	if (s.back().ry == ny) return;
	if (s.back().delta == +1 || s.back().ly == s.back().ry) {
		s.back().ry = ny;
		s.back().delta = +1;
	} else {
		s.PB(Line(s.back().ry, ny, s.back().rcost(), +1));
	}
}

void shift(Seg& s, int dt) {
	assert(dt >= 0);
	if (dt == 0) return;
	assert(SZ(s) >= 1);
	if (s[0].ly != -INF) lextend(s, s[0].ly - dt);
	if (s.back().ry != INF) rtrim(s, s.back().ry - dt);
}

void kill(int ly, int ry) {
	//printf("killing %d..%d\n", ly, ry);
	assert(ly < ry);
	REPSZ(i, segs) {
		if (ly < segs[i][0].ly && ry > segs[i].back().ry) {
			segs.erase(segs.begin() + i);
			--i;
			continue;
		}
		if (ly < segs[i][0].ly && ry > segs[i][0].ly) {
			ltrim(segs[i], ry);
			continue;
		}
		if (ry > segs[i].back().ry && ly < segs[i].back().ry) {
			rtrim(segs[i], ly);
		}
		if (ly >= segs[i][0].ly && ry <= segs[i].back().ry) {
			segs.insert(segs.begin() + i + 1, segs[i]);
			rtrim(segs[i], ly);
			ltrim(segs[i + 1], ry);
			++i;
		}
	}
	//printf("\t"); for (const auto s : segs) { printf(" ["); REPSZ(i, s) { if (i != 0) printf("="); printf("(%d,%d)(%d,%d)", s[i].ly, s[i].lcost, s[i].ry, s[i].rcost()); } printf("]"); } puts("");
}
void augment(int t) {
	map<int, int> delta;
	REP(i, nrect) if (rect[i].lt < t && t < rect[i].rt) delta[rect[i].lx - t]++, delta[rect[i].rx - t]--;
	int bal = 0, start = INT_MIN;
	vector<pair<int, int>> blocked;
	for (auto z : delta) {
		int obal = bal;
		bal += z.second;
		if (obal == 0 && bal > 0) start = z.first;
		if (obal > 0 && bal == 0) blocked.PB(MP(start, z.first));
	}
	//printf("\tblocked %d:", t); REPSZ(i, blocked) printf(" %d..%d", blocked[i].first, blocked[i].second); puts("");

	REPSZ(i, segs) {
		{
			auto it = lower_bound(blocked.begin(), blocked.end(), MP(segs[i][0].ly, INT_MIN));
			int ny = -INF;
			if (i - 1 >= 0) ny = max(ny, segs[i - 1].back().ry);
			if (it != blocked.begin()) {
				it = prev(it);
				if (it->second <= segs[i][0].ly) ny = max(ny, it->second); else ny = segs[i][0].ly;
			}
			lextend(segs[i], ny);
		}
		{
			auto it = lower_bound(blocked.begin(), blocked.end(), MP(segs[i].back().ry, INT_MIN));
			if (it != blocked.begin() && prev(it)->second > segs[i].back().ry) it = prev(it);
			int ny = +INF;
			if (i + 1 < SZ(segs)) ny = min(ny, segs[i + 1][0].ly);
			if (it != blocked.end()) if (it->first >= segs[i].back().ry) ny = min(ny, it->first); else ny = segs[i].back().ry;
			rextend(segs[i], ny);
		}
	}
	//printf("\tmerge%d:", t); for (const auto s : segs) { printf(" ["); REPSZ(i, s) { if (i != 0) printf("="); printf("(%d,%d)(%d,%d)", s[i].ly, s[i].lcost, s[i].ry, s[i].rcost()); } printf("]"); } puts("");
	REP(i, SZ(segs) - 1) {
		auto& l = segs[i];
		auto& r = segs[i + 1];
		if (l.back().ry != r[0].ly) continue;
		while (SZ(l) > 0 && SZ(r) > 0 && l.back().rcost() > r[0].lcost) {
			if (l.back().delta == -1 || l.back().ly == l.back().ry) {
				lextend(r, l.back().ly);
				l.pop_back();
			} else if (l.back().delta == +1) {
				if (r[0].lcost + l.back().ry - l.back().ly <= l.back().lcost) {
					lextend(r, l.back().ly);
					l.pop_back();
				} else {
					//l.back().lcost+(y-l.back().ly)=r[0].lcost+(l.back().ry-y) -> y=(l.back().ly+l.back().ry+r[0].lcost-l.back().lcost)/2
					int ny = l.back().ly + l.back().ry + r[0].lcost - l.back().lcost; assert(ny % 2 == 0); ny /= 2;
					lextend(r, ny);
					rtrim(l, ny);
					assert(l.back().rcost() == r[0].lcost);
				}
			} else {
				assert(false);
			}
		}
		while (SZ(l) > 0 && SZ(r) > 0 && l.back().rcost() < r[0].lcost) {
			if (r[0].delta == +1 || r[0].ly == r[0].ry) {
				rextend(l, r[0].ry);
				r.erase(r.begin());
			} else if (r[0].delta == -1) {
				if (l.back().rcost() + r[0].ry - r[0].ly <= r[0].rcost()) {
					rextend(l, r[0].ry);
					r.erase(r.begin());
				} else {
					// r[0].lcost-(y-r[0].ly)=l.back().rcost()+(y-r[0].ly) -> y=(r[0].ly+r[0].ly+r[0].lcost-l.back().rcost())/2
					int ny = r[0].ly + r[0].ly + r[0].lcost - l.back().rcost(); assert(ny % 2 == 0); ny /= 2;
					rextend(l, ny);
					ltrim(r, ny);
					assert(l.back().rcost() == r[0].lcost);
				}
			} else {
				assert(false);
			}
		}
		if (SZ(l) == 0) { segs.erase(segs.begin() + i); --i; continue; }
		if (SZ(r) == 0) { segs.erase(segs.begin() + i + 1); --i; continue; }
		assert(l.back().ry == r[0].ly);
		assert(l.back().rcost() == r[0].lcost);
		REPSZ(i, r) l.PB(r[i]);
		segs.erase(segs.begin() + i + 1); --i;
	}


}


int solve() {
	//REP(i, nrect) printf("t=%d..%d y=%d..%d to %d..%d\n", rect[i].lt, rect[i].rt, rect[i].lx - rect[i].lt, rect[i].rx - rect[i].lt, rect[i].lx - rect[i].rt, rect[i].rx - rect[i].rt);

	segs.clear();
	Seg initseg; initseg.PB(Line(-INF, sx, abs(-INF - sx), -1)); initseg.PB(Line(sx, +INF, 0, +1)); segs.PB(initseg);

	e.clear();
	REP(i, nrect) e.PB(rect[i].lt), e.PB(rect[i].rt);
	sort(e.begin(), e.end());
	e.erase(unique(e.begin(), e.end()), e.end());

	int pt = 0;
	for (int t : e) {
		REPSZ(i, segs) shift(segs[i], t - pt);
		//printf("after shifting %d:", t); for (const auto s : segs) { printf(" ["); REPSZ(i, s) { if (i != 0) printf("="); printf("(%d,%d)(%d,%d)", s[i].ly, s[i].lcost, s[i].ry, s[i].rcost()); } printf("]"); } puts("");
		augment(t);
		//printf("after augmenting %d:", t); for (const auto s : segs) { printf(" ["); REPSZ(i, s) { if (i != 0) printf("="); printf("(%d,%d)(%d,%d)", s[i].ly, s[i].lcost, s[i].ry, s[i].rcost()); } printf("]"); } puts("");
		REP(i, nrect) if (rect[i].lt == t) kill(rect[i].lx - t, rect[i].rx - t);

		//printf("after %d:", t); for (const auto s : segs) { printf(" ["); REPSZ(i, s) { if (i != 0) printf("="); printf("(%d,%d)(%d,%d)", s[i].ly, s[i].lcost, s[i].ry, s[i].rcost()); } printf("]"); } puts("");
		pt = t;
	}
	assert(SZ(segs) == 1);
	int ret = INT_MAX;
	for (auto l : segs[0]) ret = min(ret, min(l.lcost, l.rcost()));
	return ret;
}

void run() {
	scanf("%d", &nrect);
	scanf("%d", &sx);
	REP(i, nrect) scanf("%d%d%d%d", &rect[i].lt, &rect[i].rt, &rect[i].lx, &rect[i].rx), --rect[i].lt, ++rect[i].rt, --rect[i].lx, ++rect[i].rx;
	printf("%d\n", solve());
}

int main() {
	run();
	return 0;
}