//by xxj
//I love 7 sea k fall
#include<bits/stdc++.h>
using namespace std;
#define fst first
#define snd second
#define mp make_pair
#define ll long long
#define pii pair<int,int>
#define lowbit(x) x&-x
const int inf=1e9+7;
const double eps=1e-10;
const ll linf=1e18+7;
const ll hh=523;
inline int read(){
//const int mod=;
    int x=0;char ch=getchar();
    while(ch<'0'||ch>'9')ch=getchar();
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x;
}
const int N=1000007;
int L[200007],R[100007];
int le[1000007],ri[1000007];
int dat[1000007],lazy[1000007];
int wz[200007];
int S,T;
struct Edge{
	int next,to,cap;
}edge[80000007];
int len=0;
int head[1200007],level[1200007],laber[1200007];
void addedge(int x,int y,int c){
	edge[len].next=head[x];
	edge[len].to=y;
	edge[len].cap=c;
	head[x]=len++;
	edge[len].next=head[y];
	edge[len].to=x;
	edge[len].cap=0;
	head[y]=len++;
}
void build(int l,int r,int id){
	le[id]=l;
	ri[id]=r;
	if (l==r){
		return;
	}
	int mid=(l+r)>>1;
	build(l,mid,id*2);
	build(mid+1,r,id*2+1);
	return;
}
void push_down(int id){
	if (lazy[id]){
		dat[id*2]+=lazy[id];
		dat[id*2+1]+=lazy[id];
		lazy[id*2]+=lazy[id];
		lazy[id*2+1]+=lazy[id];
		lazy[id]=0;
	} 
}
void add(int l,int r,int id){
	int l1=le[id],r1=ri[id];
	if (l>r1 || l1>r){
		return;
	}
	if (l<=l1 && r1<=r){
		dat[id]++;
		lazy[id]++;
		return;
	}
	push_down(id);
	add(l,r,id*2);
	add(l,r,id*2+1);
	dat[id]=max(dat[id*2],dat[id*2+1]);
}
void nxtbuild(int id){
	if (le[id]==ri[id]){
		addedge(id,T,1);
		return;
	}
	push_down(id);
	addedge(id,id*2,min(dat[id*2],ri[id*2]-le[id*2]+1));
	addedge(id,id*2+1,min(dat[id*2+1],ri[id*2+1]-le[id*2+1]+1));
	nxtbuild(id*2);
	nxtbuild(id*2+1);
}
void findit(int v,int l,int r,int id){
	int l1=le[id],r1=ri[id];
	if (l1>r || l>r1){
		return;
	}
	if (l<=l1 && r1<=r){
		addedge(v,id,r1-l1+1);
		return;
	}
	findit(v,l,r,id*2);
	findit(v,l,r,id*2+1);
}
queue<int> q;
bool bfs(){
	memset(level,-1,sizeof(level));
	while (!q.empty()){
		q.pop();
	}
	level[S]=0;
	q.push(S);
	while (!q.empty()){
		int v=q.front();
		q.pop();
		for (int i=head[v];i!=-1;i=edge[i].next){
			int u=edge[i].to;
			if (level[u]==-1 && edge[i].cap!=0){
				level[u]=level[v]+1;
				if (u==T){
					return true;
				}
				q.push(u);
			}
		}
	}
	return false;
}
int dfs(int v,int f){
	if (v==T || !f){
		return f;
	}
	int re=0;
	for (int i=head[v];i!=-1;i=edge[i].next){
		int u=edge[i].to;
		if (level[u]>level[v]){
			int d=dfs(u,min(edge[i].cap,f));
			if (d){
				f-=d;
				re+=d;
				edge[i].cap-=d;
				edge[i^1].cap+=d;
				if (!f){
					return re;
				}
			}
		}
	}
	if (!re){
		level[v]=-1;
	}
	return re;
}
int dinic(){
	int re=0;
	while (bfs()){
		memcpy(laber,head,sizeof(laber));
		int f=dfs(S,inf);
		while (f){
			re+=f;
			f=dfs(S,inf);
		}
	}
	return re;
}
int main(){
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	int n,m;
	scanf("%d%d",&n,&m);
	build(1,m,1);
	S=0;
	memset(head,-1,sizeof(head));
	T=N+n+m+1;
	for (int i=0;i<n;i++){
		scanf("%d%d",L+i,R+i);
		if (L[i]>=1){
			add(1,L[i],1);
		}
		if (R[i]<=m){
			add(R[i],m,1);
		}
	}
	nxtbuild(1);
	for (int i=0;i<n;i++){
		addedge(S,N+i,1);
//		cout<<"n: "<<i<<' '<<S<<' '<<N+i<<' '<<1<<endl;
	}
	for (int i=0;i<n;i++){
		if (L[i]>=1){
			findit(N+i,1,L[i],1);
		}
		if (R[i]<=m){
			findit(N+i,R[i],m,1);
		}
	}
	printf("%d\n",n-dinic());
	return 0;
}
/*
input:
*/
