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>
#define rep(__i, l, r) for(int __i = (int)(l);__i <= (int)(r);++__i)
//#define M 26
#define fi first
#define se second
#define PB push_back
#define MP make_pair
#define ls (rt<<1)
#define rs (rt<<1|1)
using namespace std;
typedef long long ll;
//#define double long double
typedef unsigned long long ull;
//typedef __int128 LLL;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 1e9 + 10;
const int maxn = 1e6 + 200;
const int mod = 1e9 + 7;
const double eps = 1e-9;
int dcmp(double x) {
	if (fabs(x) < eps) return 0;
	return x < 0 ? -1 : 1;
}
ll powmod(ll x, ll n) {
	ll ret = 1;
	for (; n > 0; n >>= 1, x = x * x % mod)if (n & 1) ret = ret * x % mod;
	return ret;
}
struct net_flow {
		static const int N = 2e5 + 100;
		static const int M = 3e6 + 100;
		struct Edge {
				int to, w, flow, next;
		} E[M << 1];
		int P[N], tot, n, deep[N];
		void init(int _n) {
			n = _n;
			tot = 0, memset(P, -1, (n + 2) << 2);
		}
		void add(int u, int v, int flow) {
			E[tot].to = v, E[tot].next = P[u], E[tot].flow = flow;
			P[u] = tot++;

			E[tot].to = u, E[tot].next = P[v], E[tot].flow = 0;
			P[v] = tot++;
		}
		bool bfs(int start, int end) {
			memset(deep, 0, (n + 5) << 2);
			static int q[N << 2];
			int l = 0, r = -1;
			q[++r] = start, deep[start] = 1;
			while (l <= r) {
				int now = q[l++];
				for (int i = P[now]; ~i; i = E[i].next) {
					Edge u = E[i];
					if (!deep[u.to] && u.flow) {
						deep[u.to] = deep[now] + 1;
						q[++r] = u.to;
					}
				}
			}
			return deep[end];
		}
		int dfs(int start, int flow, int &end) {
			if (start == end) return flow;
			int f = 0;
			for (int i = P[start]; ~i && flow; i = E[i].next) {
				Edge &u = E[i];
				if (deep[start] + 1 == deep[u.to] && u.flow) {
					int r = dfs(u.to, min(flow, u.flow), end);
					u.flow -= r;
					E[i ^ 1].flow += r;
					f += r, flow -= r;
				}
			}
			if (!f)deep[start] = -2;
			return f;
		}
		int max_flow(int S, int T) {
			int ret = 0;
			while (bfs(S, T)) {
				ret += dfs(S, inf, T);
			}
			return ret;
		}
} flow;
int main() {
	int n, m;
	cin >> n >> m;
	swap(n, m);
	flow.init(n + m * 3 + 10);
	for (int i = 1; i <= m; ++i) {
		int x, y;
		cin >> x >> y;
		for (int j = 1; j <= x; ++j) {
			flow.add(i + m, j + m * 2, 1);
		}
		for (int j = y; j <= m; ++j) {
			flow.add(i + m, j + m * 2, 1);
		}
	}
	for (int i = 1; i <= m; ++i) {
		flow.add(0, i, 1);
		flow.add(i, i + m, 1);
	}
	for (int i = 1; i <= n; ++i) {
		flow.add(i + m * 3, n + m * 3 + 1, 1);
		flow.add(i + m * 2, i + m * 3, 1);
	}
	int ans = flow.max_flow(0, n + m * 3 + 1);
	cout << m - ans << '\n';
	return 0;
}