#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#include <functional>
#include <cmath>
#include <string>
#include <set>
#include <map>
#define SIZE 200005

using namespace std;
typedef long long int ll;
typedef pair <int,int> P;

vector <int> query[SIZE];
int nm[SIZE];
int n,m;

bool check(int X)
{
	priority_queue <int,vector <int>,greater <int> > que;
	memset(nm,0,sizeof(nm));
	int cnt=X;
	int sum=0;
	for(int i=0;i<=m+1;i++)
	{
		sum+=nm[i];
		if(1<=i&&i<=m) cnt++;
		for(int j=0;j<query[i].size();j++)
		{
			int to=query[i][j];
			if(cnt>0)
			{
				cnt--;
				que.push(to);
			}
			else
			{
				que.push(to);
				int tp=que.top();que.pop();
				if(tp<=i) sum++;
				else nm[tp]++;
			}
		}
		if(1<=i&&i<=m&&cnt>=1&&sum>=1)
		{
			cnt--;
			sum--;
		}
	}
	return sum==0;
}
int main()
{
	scanf("%d %d",&n,&m);
	for(int i=0;i<n;i++)
	{
		int l,r;
		scanf("%d %d",&l,&r);
		query[l].push_back(r);
	}
	int low=-1,up=n;
	while(up-low>1)
	{
		int d=(up+low)/2;
		if(check(d)) up=d;
		else low=d;
	}
	printf("%d\n",up);
	return 0;
}
