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

#define N	2048
#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 y_, head, tail, next[N * 2], prev[N * 2], xy[N * 2][N * 2], kk[N * 2]; long long dd[N * 2][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, k;

	if (kk[i] == 0 || xy[i][kk[i] - 1] <= (z = xx[next[i]] - y_))
		return;
	k = kk[i];
	while (k && xy[i][k - 1] > z)
		k--;
	if (k == 0 || dd[i][k] + xy[i][k] - z < dd[i][k - 1] + z - xy[i][k - 1])
		dd[i][k] += xy[i][k] - z, xy[i][k] = z, k++;
	kk[i] = k;
}

void insert(int i) {
	int l, z, h;
	long long d1, d2;

	l = head;
	while (cmp(next[l], i) < 0)
		l = next[l];
	if (kk[l]) {
		refresh(l);
		z = xx[i] - y_;
		h = 0;
		while (h < kk[l] && xy[l][h] <= z)
			h++;
		d1 = h == 0 ? INF : dd[l][h - 1] + z - xy[l][h - 1], d2 = h == kk[l] ? INF : dd[l][h] + xy[l][h] - z;
		if (d1 < d2) {
			kk[i] = kk[l] - h + 1, kk[l] = h;
			xy[i][0] = z, dd[i][0] = d1;
			if (kk[i] - 1 > 0) {
				memcpy(xy[i] + 1, xy[l] + h, (kk[i] - 1) * sizeof *xy[l]);
				memcpy(dd[i] + 1, dd[l] + h, (kk[i] - 1) * sizeof *dd[l]);
			}
		} else if (d1 > d2) {
			kk[i] = kk[l] - h, kk[l] = h + 1;
			memcpy(xy[i], xy[l] + h, kk[i] * sizeof *xy[l]);
			memcpy(dd[i], dd[l] + h, kk[i] * sizeof *dd[l]);
			xy[l][kk[l] - 1] = z, dd[l][kk[l] - 1] = d2;
		} else {
			kk[i] = kk[l] - h, kk[l] = h;
			memcpy(xy[i], xy[l] + h, kk[i] * sizeof *xy[l]);
			memcpy(dd[i], dd[l] + h, kk[i] * sizeof *dd[l]);
		}
	}
	next[i] = next[l], prev[next[l]] = i, next[l] = i, prev[i] = l;
}

void extract(int i) {
	int l = prev[i], h1, h2;

	refresh(l), refresh(i);
	h1 = kk[l] - 1, h2 = 0;
	while (h1 >= 0 && h2 < kk[i]) {
		int z = xy[i][h2] - xy[l][h1];

		if (dd[l][h1] >= dd[i][h2] + z)
			h1--;
		else if (dd[l][h1] + z <= dd[i][h2])
			h2++;
		else
			break;
	}
	kk[l] = h1 + 1;
	if (h2 < kk[i]) {
		memcpy(xy[l] + kk[l], xy[i] + h2, (kk[i] - h2) * sizeof *xy[i]);
		memcpy(dd[l] + kk[l], dd[i] + h2, (kk[i] - h2) * sizeof *dd[i]);
		kk[l] += kk[i] - h2;
	}
	next[l] = next[i], prev[next[i]] = l;
}

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

	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;
	xy[head][kk[head]] = x, dd[head][kk[head]] = 0, kk[head]++;
	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])
				kk[i] = 0;
		} else
			extract(i0), extract(i1);
	}
	ans = INF;
	for (h = 0; h < kk[head]; h++)
		ans = min(ans, dd[head][h]);
	printf("%lld\n", ans);
	return 0;
}