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>
using namespace std;
typedef double db;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
#define dd(x) cout << #x << " = " << x << ", "
#define de(x) cout << #x << " = " << x << endl
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define per(i,a,b) for(int i=(b)-1;i>=(a);--i)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
//INF = 1 061 109 567 ; INF + INF = 2 122 219 134 ;
//LINF = 4 557 430 888 798 830 399 ; LINF + LINF = 9 114 861 777 597 660 798 ;
const int INF = 0x3f3f3f3f;     // -INF: 0xc0   abs(-INF) = INF + 1
const ll LINF = 0x3f3f3f3f3f3f3f3f;
/*}}}*/                         // zr, zm
const int MOD = (int)1e9+7;     // when MOD = 1e9+7, inv2 = 500000004
const int N = (int)2e5+7;
int lazy[N<<2],tree[N<<2];
#define ls rt << 1
#define rs rt << 1 | 1
#define lson l, m, ls
#define rson m+1, r, rs
void Add(int rt,int val) {
	lazy[rt] += val;
	tree[rt] += val;
}
void pushdown(int rt) {
	if(lazy[rt]) {
		Add(ls,lazy[rt]);
		Add(rs,lazy[rt]);
		lazy[rt] = 0;
	}
}
void update(int ql,int qr,int val,int l,int r,int rt) {
	if(ql <= l && qr >= r) {
		Add(rt,val);return;
	}
	pushdown(rt);
	int m = (l + r) >> 1;
	if(ql <= m) update(ql,qr,val,lson);
	if(m < qr) update(ql,qr,val,rson);
	tree[rt] = max(tree[ls],tree[rs]);
}
int query(int ql,int qr,int l,int r,int rt) {
	if(ql <= l && qr >= r) return tree[rt];
	pushdown(rt);
	int m = (l + r) >> 1;
	int res = 0;
	if(ql <= m) res = max(res,query(ql,qr,lson));
	if(m < qr) res = max(res,query(ql,qr,rson));
	return res;
}
pii p[N];
int main()
{
	int n,m;scanf("%d%d",&n,&m);
	rep(i,0,n) {
		scanf("%d%d",&p[i].fi,&p[i].se);
	}
	sort(p,p+n);
	int ans = 0;
	rep(i,1,m+2) {
		update(i,i,i-1,1,m+1,1);
	}
	for(int i = 0,j;i<n;i=j) {
		for(j = i;j < n && p[i].fi == p[j].fi;j++);
		per(k,i,j) {
			update(1,p[k].se,1,1,m+1,1);
		}
		ans = max(ans,query(1,m+1,1,m+1,1) - p[i].fi - m);
	}
	printf("%d\n",ans);
	return 0;
}