#include "bits/stdc++.h"
using namespace std;
const int N = 2e5 + 5;
int n , m;
int l[N] , r[N];
vector < int > v[N];
priority_queue < int > pq;
int cnt[N];
bool check(int val){
	while(!pq.empty()){
		pq.pop();
	}
	for(int i = 0 ; i <= m + 1 ; ++i){
		cnt[i] = 0;
	}
	int tot = 0;
	int mx = 0;
	for(int i = 0 ; i <= m ; ++i){
		tot += i ? 1 : val;
		for(int r : v[i]){
			pq.push(-r);
		}
		while(pq.size() > tot){
			++cnt[-pq.top()];
			pq.pop();
		}
	}
	mx = pq.size();
	if(cnt[m + 1]){
		return 0;
	}
	tot = 0;
	int s = 0;
	for(int i = m ; i >= 1 ; --i){
		s += cnt[i];
		++tot;
		if(s > tot){
			return 0;
		}
	}
	s = m - s;
	return mx <= val + s;
}
int main(){
	scanf("%d %d" , &n , &m);
	for(int i = 1 ; i <= n ; ++i){
		scanf("%d %d" , l + i , r + i);
		v[l[i]].emplace_back(r[i]);
	}
	int l = 0;
	int r = n;
	while(l < r){
		int mid = l + r >> 1;
		if(check(mid)){
			r = mid;
		}
		else{
			l = mid + 1;
		}
	}
	printf("%d\n" , l);
}