// Slower C++ solution:
#include<cstdio>

#include<cstring>

#include<iostream>

#include<queue>

#include<vector>

using namespace std;

struct node

{

    long long v,tag;

}tree[1000000*4+1];

void build(int node,int l,int r)

{

    if(l==r)

    {

        tree[node].v=l;

        return;

    }

    build(node*2,l,(l+r)/2);

    build(node*2+1,(l+r)/2+1,r);

    tree[node].v=max(tree[node*2].v,tree[node*2+1].v);

}

void pushdown(int node,int l,int r)

{

    if(tree[node].tag)

    {

        tree[node*2].v+=tree[node].tag;

        tree[node*2+1].v+=tree[node].tag;

        tree[node*2].tag+=tree[node].tag;

        tree[node*2+1].tag+=tree[node].tag;

        tree[node].tag=0;

    }

}

long long query(int node,int st,int ed,int l,int r)

{

    if(st>r||ed<l)

        return -1;

    if(st>=l&&ed<=r)

        return tree[node].v;

    pushdown(node,st,ed);

    return max(query(node*2,st,(st+ed)/2,l,r),query(node*2+1,(st+ed)/2+1,ed,l,r));

}

void update(int node,int st,int ed,int l,int r,int ind)

{

    if(st>r||ed<l)

        return;

    if(st>=l&&ed<=r)

    {

        tree[node].v+=ind;

        tree[node].tag+=ind;

        return;

    }

    pushdown(node,st,ed);

    update(node*2,st,(st+ed)/2,l,r,ind);

    update(node*2+1,(st+ed)/2+1,ed,l,r,ind);

    tree[node].v=max(tree[node*2].v,tree[node*2+1].v);

}

vector<int> p[200001];

int main()

{

    long long n,m; 

    cin>>n>>m;

    for(int i=1;i<=n;i++)

	{

		int l,r;

		scanf("%d%d",&l,&r);

		p[l].push_back(r);

    }

	long long ans=n-m;

    build(1,1,m+1);

	for(int i=0;i<=m;i++)

	{

		for(int j=0;j<p[i].size();j++)

		{

			int right=p[i][j];

			update(1,1,m+1,1,right,1);

		}

		ans=max(ans,query(1,1,m+1,i+1,m+1)-m-i-1);

	}

	printf("%lld",ans);

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 6.458696250002861x):
#include<stdio.h>

#include<bits/stdc++.h>

#define f(a,b,c) for(register int a=(b);a<=(c);++a)

#define ff(a,b,c) for(register int a=(b);a>=(c);--a)

#define ll long long

char pbuf[1<<20],*pp1=pbuf;

inline void push(char c){*pp1=c;pp1=(pp1-pbuf==(1<<20)-1)?(fwrite(pbuf,1,1<<20,stdout),pbuf):(pp1+1);}

#define pc push

//#define pc putchar

#define pe pc('\n')

#define ps pc(' ')

#define wer rd()

char *p1,*p2,buf[1<<20];

#define GC (p1==p2&&(p1=buf,p2=buf+fread(buf,1,1<<20,stdin),p1==p2)?0:(*(p1++)))

//#define GC getchar()

using namespace std;

inline ll wer{

	ll ans;char t,k;

	while(((t=GC)!='-')&&(t>'9'||t<'0'));

	k=(t=='-');

	ans=k?0:(t-'0');

	while((t=GC)>='0'&&t<='9')ans=ans*10+t-'0';

	return k?-ans:ans;

}

inline void wt(ll k)

{

	if(k<0)pc('-'),wt(-k);

	else

	{

		if(k<10)pc('0'+k);

		else wt(k/10),pc('0'+k%10);

	}

	return;

}

pair<int,int>a[200001];

priority_queue<int>q;

int n,m;

vector<int>v;

int main()

{

	scanf("%d%d",&n,&m);

	f(i,1,n)scanf("%d%d",&a[i].first,&a[i].second);

	sort(a+1,a+n+1,[&](pair<int,int>a,pair<int,int>b)

	{

		if(a.first==b.first)return a.second>b.second;

		return a.first<b.first;

	});

	int le=1,ri=m;

	f(i,1,n)

	{

		if(le>a[i].first)

		{

			if(q.size()&&q.top()<a[i].second)

			{

				int t=q.top();

				q.pop();

				v.push_back(t);

			}

			else v.push_back(a[i].second);

		}

		else le++;

	}

	int Ans=0;

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

	reverse(v.begin(),v.end());

	for(int a:v)

	{

		if(ri<a||le>ri)Ans++;

		else ri--;

	}

	cout<<Ans;

}