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≤ 2 ⋅ 10^5) — 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 N_	(1 << 19)	/* N_ = pow2(ceil(log2(N * 2))) */
#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], xx_[N * 2], yy[N * 2], inv[N * 2];

int *aa;

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 = aa[ii[j]] != aa[i_] ? aa[ii[j]] - aa[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];

char st1[N_ * 2], st2[N_ * 2]; int n_;

void pul(int i) {
	int l = i << 1, r = l | 1;

	st1[i] = st1[l] || st1[r], st2[i] = st2[l] || st2[r];
}

void pull(int i) {
	while (i > 1)
		pul(i >>= 1);
}

void update(int i, int x) {
	i = xx_[i];
	st1[n_ + i] = x, st2[n_ + i] = st1[n_ + i] && tt[inv[i]];
	pull(i + n_);
}

int floor_(int i) {
	int r = xx_[i] + n_;

	while (1) {
		if ((r & 1) == 0) {
			if (st1[r]) {
				i = r;
				while (i < n_)
					i = st1[i << 1 | 1] ? i << 1 | 1 : i << 1 | 0;
				return inv[i - n_];
			}
			r--;
		}
		r >>= 1;
	}
}

void clear_(int i) {
	if (st2[i]) {
		st2[i] = 0;
		if (i >= n_)
			tt[inv[i - n_]] = 0;
		else
			clear_(i << 1 | 0), clear_(i << 1 | 1);
	}
}

void clear(int l, int r) {
	int l_ = l += n_, r_ = r += n_;

	for ( ; l <= r; l >>= 1, r >>= 1) {
		if ((l & 1) == 1)
			clear_(l++);
		if ((r & 1) == 0)
			clear_(r--);
	}
	pull(l_), pull(r_);
}

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, ll[v] = rr[v] = 0, l_ = merge(l_, v);
	}
	tt[i] = l_;
}

void insert(int i) {
	int p = floor_(i);

	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));
	}
	update(p, 1), update(i, 1), next[i] = next[p], prev[next[p]] = i, next[p] = i, prev[i] = p;
}

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

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

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 + 2; i++)
		inv[i] = i;
	aa = xx, sort(inv, 0, n * 2 + 2);
	for (i = 0; i < n * 2 + 2; i++)
		xx_[inv[i]] = i;
	n_ = 1;
	while (n_ < n * 2 + 2)
		n_ <<= 1;
	for (i = 0; i < n * 2; i++)
		ii[i] = i;
	aa = yy, sort(ii, 0, n * 2);
	head = n << 1 | 0, tail = n << 1 | 1;
	next[head] = tail, prev[tail] = head, tt[head] = node(x, 0);
	update(head, 1), update(tail, 1);
	for (h = 0; h < n * 2; h++) {
		i = ii[h] >> 1, y_ = yy[ii[h]];
		if ((ii[h] & 1) == 0)
			insert(i << 1 | 0), insert(i << 1 | 1), clear(xx_[i << 1 | 0], xx_[i << 1 | 1] - 1);
		else
			extract(i << 1 | 0), extract(i << 1 | 1);
	}
	printf("%lld\n", getans(tt[head]));
	return 0;
}