#include<bits/stdc++.h>
#define FASTIO ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
#define lson ((p)<<1)
#define rson ((p)<<1|1)
#define ft first
#define sd second
#define pb emplace_back
#define dbg cout << "^^^\n";
#define int long long
using namespace std;
using ll = long long;
using pii = pair<int,int>;
const int INF=0x3f3f3f3f;
const int MAX=1e5+20;
vector<int> alls;
vector<pii> opt;
int vis[MAX];
int ans=0;
int find(int x){
int l=1,r=(int)alls.size()-1;
while(l<r){
int mid=(l+r+1)>>1;
if(alls[mid]<=x) l=mid;
else r=mid-1;
}
return l;
}
struct segment_tree{
struct{
int l,r;
int color=0;
int lztag=0;
}tr[MAX*4];
void build(int p,int lo,int ro){
tr[p].l=lo,tr[p].r=ro,tr[p].color=0,tr[p].lztag=0;
if(tr[p].l==tr[p].r) return;
int mid=(lo+ro)>>1;
build(lson,lo,mid);
build(rson,mid+1,ro);
}
void push_down(int p){
if(tr[p].lztag==0) return;
int tag=tr[p].lztag;
tr[lson].color=tag,tr[lson].lztag=tag;
tr[rson].color=tag,tr[rson].lztag=tag;
tr[p].lztag=0;
}
void modify(int p,int lo,int ro,int k){
if(tr[p].l>=lo && tr[p].r<=ro){
tr[p].color=k;
tr[p].lztag=k;
return;
}
push_down(p);
int ndmid=(tr[p].l+tr[p].r)>>1;
if(lo<=ndmid) modify(lson,lo,ro,k);
if(ro>ndmid) modify(rson,lo,ro,k);
if(tr[lson].color==tr[rson].color) tr[p].color=tr[lson].color;
else tr[p].color=-1;
}
void query(int p,int lo,int ro){
if(tr[p].color!=-1 && tr[p].l>=lo && tr[p].r<=ro){
if(!vis[tr[p].color]) ++ans,vis[tr[p].color]=true;
return;
}
push_down(p);
int ndmid=(tr[p].l+tr[p].r)>>1;
if(lo<=ndmid) query(lson,lo,ro);
if(ro>ndmid) query(rson,lo,ro);
}
};
segment_tree sgt;
void solve(){
int n,m;
cin >> n >> m;
opt.clear();
alls.clear();
alls.pb(-INF);
for(int i=1;i<=n;++i){
int l,r;
cin >> l >> r;
if(l>r) swap(l,r);
opt.pb(make_pair(l,r));
alls.pb(l); alls.pb(r); alls.pb(r+1);
}
sort(alls.begin()+1,alls.end());
alls.erase(unique(alls.begin()+1,alls.end()),alls.end());
sgt.build(1,0,(int)alls.size()+5);
ans=0;
vis[0]=true;
int k=0;
for(auto [l,r]:opt){
sgt.modify(1,find(l),find(r),++k);
}
sgt.query(1,0,(int)alls.size()+5);
cout << ans << '\n';
return;
}
signed main(){
solve();
return 0;
}