/**
 * @author Macesuted (macesuted@qq.com)
 * @copyright Copyright (c) 2021
 */

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define mod 1000000007
// #define mod 998244353
#define maxn 200005

long long fac[maxn], inv[maxn];

long long power(long long a, long long x) {
    long long ans = 1;
    while (x) {
        if (x & 1) ans = ans * a % mod;
        a = a * a % mod;
        x >>= 1;
    }
    return ans;
}

inline long long C(int n, int m) { return fac[n] * inv[m] % mod * inv[n - m] % mod; }
void INIT(void) {
    fac[0] = inv[0] = 1;
    for (register int i = 1; i < maxn; i++) fac[i] = fac[i - 1] % mod;
    inv[maxn - 1] = power(fac[maxn - 1], mod - 2);
    for (register int i = maxn - 2; i; i--) inv[i] = inv[i + 1] * (i + 1) % mod;
    return;
}

typedef pair<int, int> pii;

map<int, int> rec, cac;

pii a[maxn];

int n, m;

void work(void) {
    // INIT();
    cin >> n >> m;
    for (register int i = 1; i <= m; i++) cin >> a[i].first >> a[i].second;
    sort(a + 1, a + m + 1);
    rec[n] = 1;
    for (register int i = 1, last = -1; i <= m; i++) {
        if (last != a[i].first) {
            for (auto i : cac) rec[i.first] = i.second;
            cac.clear();
            last = a[i].first;
        }
        cac[a[i].second] = rec[a[i].second - 1] + rec[a[i].second + 1];
    }
    for (auto i : cac) rec[i.first] = i.second;
    int answer = 0;
    for (auto i : rec)
        if (i.second) answer++;
    cout << answer << endl;
    return;
}

signed main() {
    ios::sync_with_stdio(false);
    cout << setiosflags(ios::fixed) << setprecision(3) << endl;
    int _ = 1;
    // cin >> _;
    while (_--) work();
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using P=pair<ll,ll>;
ll N;
ll par[400005];
ll rank_[400005];
bool c[400005];
void init(ll n_){
for(ll i=0;i<n_;i++){
par[i]=i;rank_[i]=0;
}
}
ll find(ll x){
if(par[x]==x)return x;
  return par[x]=find(par[x]);
}
void unite(ll x,ll y){
x=find(x);y=find(y);
  if(x==y){c[x]=true;return;}
  if(rank_[x]<rank_[y]){
  par[x]=y;c[y]|=c[x];
  }else{
   par[y]=x;c[x]|=c[y];if(rank_[x]==rank_[y])rank_[x]++;
  }
}
int main(){
 cin>>N;set<ll>S;
  vector<P>A(N);
  for(ll i=0;i<N;i++){
  ll a,b;cin>>a>>b;S.insert(a);S.insert(b);
    A[i]=P(a,b);
  }ll I=0;map<ll,ll>mp;
  for(auto x:S){mp[x]=I;I++;}
  init(I);
  for(ll i=0;i<N;i++){
  ll a=mp.at(A[i].first);
    ll b=mp.at(A[i].second);
    unite(a,b);
  }ll ans=I;
  for(ll i=0;i<I;i++)
    if(par[i]==i){
    if(!c[i])ans--;
    }cout<<ans<<endl;
  return 0;
}