#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<cctype>
#include<cstdlib>
#include<cstring>
#include<utility>
#include<algorithm>

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define For(i,x,y) for(int i=((int)x);i<=((int)y);i++)
#define Dep(i,y,x) for(int i=((int)y);i>=((int)x);i--)
#define Rep(i,x) for (int y,i=head[x];i;i=E[i].nxt)
using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef pair<int,int> pii;

int rd() {
	char c=getchar(); int t=0,f=1;
	while (!isdigit(c)) f=(c=='-')?-1:1,c=getchar();
	while (isdigit(c)) t=t*10+c-48,c=getchar(); return t*f;
}
void wt(int x) {
	if (x<0) putchar('-'),wt(-x);
	else { if (x>9) wt(x/10); putchar(x%10+48); }
}

const int N=200035;
struct Info {
	int l,r;
	bool operator < (Info p) const {
		return l==p.l?r>p.r:l<p.l;
	}
}Q[N];

#define mid (l+r>>1)


int sum[N<<2],n,m,flg,ans;

void reset(int o,int l,int r) {
	if (l==r) { sum[o]=1; return; }
	reset(o<<1,l,mid),reset(o<<1|1,mid+1,r);
	sum[o]=sum[o<<1]+sum[o<<1|1];
}

bool solve(int o,int l,int r,int x,int y) {
	if (!sum[o] || x>y) return 0;
	if (l==x && r==y) {
		if (l==r) sum[o]--,flg=l;
		else if (sum[o<<1]) solve(o<<1,l,mid,x,mid); else solve(o<<1|1,mid+1,r,mid+1,y);
		sum[o]=sum[o<<1]+sum[o<<1|1]; return 1;
	}
	int Ret=0;
	if (y<=mid) Ret=solve(o<<1,l,mid,x,y);
	else if (x>mid) Ret=solve(o<<1|1,mid+1,r,x,y);
	else {
		
		if (solve(o<<1,l,mid,x,mid)) Ret=1;
		else Ret=solve(o<<1|1,mid+1,r,mid+1,y);
	}
	sum[o]=sum[o<<1]+sum[o<<1|1];
	return Ret;
}

bool solveR(int o,int l,int r,int x,int y) {
	if (!sum[o] || x>y) return 0;
	if (l==x && r==y) {
		if (l==r) sum[o]--,flg=l;
		else if (sum[o<<1|1]) solveR(o<<1|1,mid+1,r,mid+1,y); else solveR(o<<1,l,mid,x,mid);
		sum[o]=sum[o<<1]+sum[o<<1|1]; return 1;
	}
	int Ret=0;
	if (y<=mid) Ret=solveR(o<<1,l,mid,x,y);
	else if (x>mid) Ret=solveR(o<<1|1,mid+1,r,x,y);
	else {
		
		if (solveR(o<<1|1,mid+1,r,mid+1,y)) Ret=1;
		else Ret=solveR(o<<1,l,mid,x,mid);
	}
	sum[o]=sum[o<<1]+sum[o<<1|1];
	return Ret;
}

int main() {
	n=rd(),m=rd();
	For (i,1,n) Q[i]=(Info){rd(),rd()};
	
	reset(1,1,m);
	sort(Q+1,Q+n+1);
	
	for (int j,i=1;i<=n;i=j) {
		for (j=i;Q[i].l==Q[j].l && j<=n;j++);
		For (k,i,j-1) {
			if (solve(1,1,m,1,Q[k].l)) continue;
			if (solve(1,1,m,Q[k].r,m)) continue; ans++;
		}
	}
	
	printf("%d\n",ans);
}




