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 <stdio.h>
#include <sys/time.h>

#define N	200610
#define INF	0x3f3f3f3f3f3f3f3fLL
#define X	10000000

long long min(long long a, long long b) { return a < b ? a : b; }

unsigned int Z;

void srand_() {
	struct timeval tv;

	gettimeofday(&tv, NULL);
	Z = tv.tv_sec ^ tv.tv_usec | 1;
}

int rand_() {
	return (Z *= 3) >> 1;
}

int xx[N * 2], yy[N * 2];

void sort(int *ii, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;

		while (j < k) {
			int c = yy[ii[j]] != yy[i_] ? yy[ii[j]] - yy[i_] : (i_ & 1) - (ii[j] & 1);

			if (c == 0)
				j++;
			else if (c < 0) {
				tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
			}
		}
		sort(ii, l, i);
		l = k;
	}
}

int zz[1 + N * 2], ll[1 + N * 2], rr[1 + N * 2], xy[1 + N * 2]; long long dd[1 + N * 2]; int u_, l_, r_;

int node(int k, long long v) {
	static int _ = 1;

	zz[_] = rand_();
	xy[_] = k, dd[_] = v;
	return _++;
}

void split(int u, int k) {
	if (u == 0) {
		u_ = l_ = r_ = 0;
		return;
	}
	if (xy[u] <= k) {
		split(rr[u], k);
		rr[u] = l_, l_ = u;
	} else {
		split(ll[u], k);
		ll[u] = r_, r_ = u;
	}
}

int merge(int u, int v) {
	if (u == 0)
		return v;
	if (v == 0)
		return u;
	if (zz[u] < zz[v]) {
		rr[u] = merge(rr[u], v);
		return u;
	} else {
		ll[v] = merge(u, ll[v]);
		return v;
	}
}

int first(int u) { return ll[u] == 0 ? u : first(ll[u]); }
int last(int u) { return rr[u] == 0 ? u : last(rr[u]); }

int remove_first(int u) {
	int p, v;

	if (ll[u] == 0)
		return rr[u];
	p = u, v = ll[u];
	while (ll[v])
		p = v, v = ll[v];
	ll[p] = rr[v];
	return u;
}

int remove_last(int u) {
	int p, v;

	if (rr[u] == 0)
		return ll[u];
	p = u, v = rr[u];
	while (rr[v])
		p = v, v = rr[v];
	rr[p] = ll[v];
	return u;
}

int y_, head, tail, next[N * 2], prev[N * 2], tt[N * 2];

int cmp(int i, int j) {
	return xx[i] != xx[j] ? xx[i] - xx[j] : ((i & 1) != (j & 1) ? (j & 1) - (i & 1) : (i >> 1) - (j >> 1));
}

void refresh(int i) {
	int z = xx[next[i]] - y_;

	split(tt[i], z);
	if (r_) {
		int u = last(l_), v = first(r_);

		if (u == 0 || dd[v] + xy[v] - z < dd[u] + z - xy[u])
			dd[v] += xy[v] - z, xy[v] = z, l_ = merge(l_, v);
	}
	tt[i] = l_;
}

void insert(int i) {
	int p;

	p = head;
	while (cmp(next[p], i) < 0)
		p = next[p];
	if (tt[p]) {
		int z, u, v;
		long long d1, d2;

		refresh(p);
		split(tt[p], z = xx[i] - y_);
		u = last(l_), v = first(r_);
		d1 = u == 0 ? INF : dd[u] + z - xy[u], d2 = v == 0 ? INF : dd[v] + xy[v] - z;
		tt[p] = l_, tt[i] = r_;
		if (d1 < d2)
			tt[i] = merge(node(z, d1), tt[i]);
		else if (d1 > d2)
			tt[p] = merge(tt[p], node(z, d2));
	}
	next[i] = next[p], prev[next[p]] = i, next[p] = i, prev[i] = p;
}

void extract(int i) {
	int l = prev[i], u, v;

	refresh(l), refresh(i);
	u = last(tt[l]), v = first(tt[i]);
	while (u && v)
		if (dd[u] >= dd[v] + xy[v] - xy[u])
			u = last(tt[l] = remove_last(tt[l]));
		else if (dd[u] + xy[v] - xy[u] <= dd[v])
			v = first(tt[i] = remove_first(tt[i]));
		else
			break;
	tt[l] = merge(tt[l], tt[i]);
	next[l] = next[i], prev[next[i]] = l;
}

long long getans(int u) {
	return u == 0 ? INF : min(dd[u], min(getans(ll[u]), getans(rr[u])));
}

int main() {
	static int ii[N * 2];
	int n, x, h, i;

	srand_();
	scanf("%d%d", &n, &x);
	for (i = 0; i < n; i++)
		scanf("%d%d%d%d", &yy[i << 1 | 0], &yy[i << 1 | 1], &xx[i << 1 | 0], &xx[i << 1 | 1]), xx[i << 1 | 0]--, xx[i << 1 | 1]++, yy[i << 1 | 0]--, yy[i << 1 | 1]++;
	xx[n << 1 | 0] = -X, xx[n << 1 | 1] = X;
	for (i = 0; i < n * 2; i++)
		ii[i] = i;
	sort(ii, 0, n * 2);
	head = n << 1 | 0, tail = n << 1 | 1;
	next[head] = tail, prev[tail] = head, tt[head] = node(x, 0);
	for (h = 0; h < n * 2; h++) {
		int i0, i1;

		i = ii[h] >> 1, i0 = i << 1 | 0, i1 = i << 1 | 1, y_ = yy[ii[h]];
		if ((ii[h] & 1) == 0) {
			insert(i0), insert(i1);
			for (i = i0; i != i1; i = next[i])
				tt[i] = 0;
		} else
			extract(i0), extract(i1);
	}
	printf("%lld\n", getans(tt[head]));
	return 0;
}