Problem:
Score : 1000 points

Problem Statement

There are M chairs arranged in a line. The coordinate of the i-th chair (1 ≤ i ≤ M) is i.

N people of the Takahashi clan played too much games, and they are all suffering from backaches. They need to sit in chairs and rest, but they are particular about which chairs they sit in. Specifically, the i-th person wishes to sit in a chair whose coordinate is not greater than L_i, or not less than R_i. Naturally, only one person can sit in the same chair.

It may not be possible for all of them to sit in their favorite chairs, if nothing is done.
Aoki, who cares for the health of the people of the Takahashi clan, decides to provide additional chairs so that all of them can sit in chairs at their favorite positions.

Additional chairs can be placed at arbitrary real coordinates. Find the minimum required number of additional chairs.

Constraints

1 ≤ N,M ≤ 2 × 10^5

0 ≤ L_i < R_i ≤ M + 1(1 ≤ i ≤ N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1
:
L_N R_N

Output

Print the minimum required number of additional chairs.

Sample Input 1

4 4
0 3
2 3
1 3
3 4

Sample Output 1

0

The four people can sit in chairs at the coordinates 3, 2, 1 and 4, respectively, and no more chair is needed.

Sample Input 2

7 6
0 7
1 5
3 6
2 7
1 6
2 6
3 7

Sample Output 2

2

If we place additional chairs at the coordinates 0 and 2.5, the seven people can sit at coordinates 0, 5, 3, 2, 6, 1 and 2.5, respectively.

Sample Input 3

3 1
1 2
1 2
1 2

Sample Output 3

2

Sample Input 4

6 6
1 6
1 6
1 5
1 5
2 6
2 6

Sample Output 4

2

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include <bits/stdc++.h>

const int MAXN = 200010;
const int MAXP = MAXN * 3;
const int INF = std::numeric_limits<int>::max();
const int MAXM = MAXP * 10;
int head[MAXP], nxt[MAXM], to[MAXM], val[MAXM], tot = 1;
int S, T, idx;
void addedge(int b, int e, int v) {
	nxt[++tot] = head[b]; to[head[b] = tot] = e; val[tot] = v;
	nxt[++tot] = head[e]; to[head[e] = tot] = b; val[tot] = 0;
}
int dis[MAXP]; std::queue<int> q;
bool bfs() {
	memset(dis, 0, idx + 1 << 2);
	q.push(S); dis[S] = 1;
	while (!q.empty()) {
		int t = q.front(); q.pop();
		for (int i = head[t]; i; i = nxt[i])
			if (val[i] && !dis[to[i]]) {
				q.push(to[i]);
				dis[to[i]] = dis[t] + 1;
			}
	}
	return dis[T] > 0;
}
int dinic(int u, int minv) {
	if (!minv || u == T) return minv;
	int res = 0, t;
	for (int & i = head[u]; i; i = nxt[i])
		if (val[i] && dis[to[i]] == dis[u] + 1 && (t = dinic(to[i], std::min(minv, val[i])))) {
			val[i] -= t;
			val[i ^ 1] += t;
			res += t;
			minv -= t;
			if (!minv) break;
		}
	if (!res) dis[u] = -1;
	return res;
}
int flow() {
	static int te[MAXP];
	int ans = 0;
	memcpy(te, head, idx + 1 << 2);
	while (bfs()) {
		ans += dinic(S, INF);
		memcpy(head, te, idx + 1 << 2);
	}
	return ans;
}
int n, m;
int pre[MAXN], suc[MAXN], at[MAXN];
int main() {
	std::ios_base::sync_with_stdio(false), std::cin.tie(0);
	std::cin >> n >> m;
	S = 0, T = idx = 1;
	for (int i = 1; i <= m; ++i) {
		pre[i] = ++idx, suc[i] = ++idx, at[i] = ++idx;
		addedge(pre[i], at[i], 1);
		addedge(suc[i], at[i], 1);
		addedge(at[i], T, 1);
	}
	for (int i = 2; i <= m; ++i) addedge(pre[i], pre[i - 1], INF);
	for (int i = 1; i < m; ++i) addedge(suc[i], suc[i + 1], INF);
	for (int i = 1, l, r; i <= n; ++i) {
		std::cin >> l >> r; int now = ++idx;
		addedge(S, now, 1);
		if (l) addedge(now, pre[l], 1);
		if (r) addedge(now, suc[r], 1);
	}
	std::cout << n - flow() << std::endl;
	return 0;
}