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)

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#  include <intrin.h>
#  define __builtin_popcount __popcnt
#endif

#define _USE_MATH_DEFINES

#include <stdio.h>
#include <iostream>
#include <vector>
#include <unordered_map>
#include <stack>
#include <list>
#include <math.h>
#include <map>
#include <iomanip>
#include <functional>
#include <string>
#include <algorithm>
#include <queue>
#include <set>

using namespace std;

template<class T> ostream& operator<<(ostream& os, const vector<T>& v) {
	for (auto i = begin(v); i != end(v); i++) os << *i << (i == end(v) - 1 ? "" : " "); return os;
}
template<class T> istream& operator>>(istream& is, vector<T>& v) {
	for (auto i = begin(v); i != end(v); i++) is >> *i; return is;
}

vector<pair<int, int>> inp;

bool check(int added, int m)
{
	multiset<int> rs;
	for (auto x : inp)
	{
		rs.insert(x.second);
		while (((int)rs.size() - added) > x.first )
		{
			rs.erase(rs.find(*rs.begin()));
		}
	}

	multiset<int> all_rs;
	for (auto x : inp)
	{
		all_rs.insert(x.second);
	}

	for (auto x : rs)
	{
		all_rs.erase(all_rs.find(x));
	}

	int cnt = 0;
	while (all_rs.size() > 0)
	{
		int val = *all_rs.rbegin();
		all_rs.erase(all_rs.find(*all_rs.rbegin()));
		if (val > m)
			return false;
		cnt++;
		if ((m - val + 1) < cnt)
			return false;
	}
	return true;
}


int main() {

#if defined(_DEBUG) 
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int n, m;
//	cin >> n >> m;
	scanf("%d %d", &n, &m);
	for (int i = 0; i < n; i++)
	{
		int a, b;
//		cin >> a >> b;
		scanf("%d %d", &a, &b);
		inp.push_back({ a,b });
	}

	sort(inp.begin(), inp.end());

	int left = 0, right = (n + m + 1);
	while (left < right)
	{
		int mm = (left + right) / 2;
		if (check(mm, m))
		{
			right = mm;
		}
		else
		{
			left = mm + 1;
		}
	}

	printf("%d", left);
//	cout << left;
	return 0;
}