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: Wrong Answer)

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b , 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b , 1 : 0;}

typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;

template<typename I>
inline void read(I &x) {
	int f = 0, c;
	while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
	x = c & 15;
	while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
	f ? x = -x : 0;
}

const int N = 2e5 + 7;

int n, m;
std::vector<int> a[N];

namespace SGT {
	#define lc o << 1
	#define rc o << 1 | 1
	struct Node {int add, max;} t[N << 2];
	int a[N];
	inline void build(int o, int L, int R) {
		// dbg("build: o = %d, L = %d, R = %d\n", o, L, R);
		t[o].add = 0;
		if (L == R) return (void)(t[o].max = a[L]);
		int M = (L + R) >> 1;
		build(lc, L, M); build(rc, M + 1, R);
		t[o].max = std::max(t[lc].max, t[rc].max);
	}
	inline void pushdown(int o) {
		if (!t[o].add) return;
		t[lc].add += t[o].add, t[lc].max += t[o].add;
		t[rc].add += t[o].add, t[rc].max += t[o].add;
		t[o].add = 0;
	}
	inline void pushup(int o) {
		t[o].max = std::max(t[lc].max, t[rc].max);
		t[o].max += t[o].add;
	}
	inline void qadd(int o, int L, int R, int l, int r, int k) {
		if (l <= L && R <= r) return (void)(t[o].add += k, t[o].max += k);
		int M = (L + R) >> 1;
		pushdown(o);
		if (l <= M) qadd(lc, L, M, l, r, k);
		if (r > M) qadd(rc, M + 1, R, l, r, k);
		pushup(o);
	}
	inline int qmax(int o, int L, int R, int l, int r) {
		if (l > r) return 0;
		// dbg("o = %d, L = %d, R = %d, l = %d, r = %d\n", o, L, R, l, r);
		if (l <= L && R <= r) return t[o].max;
		int M = (L + R) >> 1;
		pushdown(o);
		if (r <= M) return qmax(lc, L, M, l, r);
		if (l > M) return qmax(rc, M + 1, R, l, r);
		return std::max(qmax(lc, L, M, l, r), qmax(rc, M + 1, R, l, r));
	}
}
using SGT::build;
using SGT::qadd;
using SGT::qmax;

inline void work() {
	++n;
	for (int i = 0; i <= n; ++i) SGT::a[i] = i - n;
	build(1, 0, n);
	int ans = std::max(0, n - m - 1);
	for (int i = 0; i <= n; ++i) {
		for (auto j : a[i]) qadd(1, 0, n, 0, j, 1);//, dbg("********* add: i = %d, j = %d\n", i, j);
		smax(ans, qmax(1, 0, n, i + 1, n) - i);
		// dbg("i = %d, ans = %d, hkk = %d\n", i, ans, qmax(1, 0, n, i, n));
	}
	printf("%d\n", ans);
}

inline void init() {
	read(m), read(n);
	for (int i = 1; i <= m; ++i) {
		int l, r;
		read(l), read(r);
		a[l].pb(r);
	}
}

int main() {
	#ifdef hzhkk
	freopen("hkk.in", "r", stdin);
	#endif
	init();
	work();
	fclose(stdin), fclose(stdout);
	return 0;
}