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

typedef long long int ll;
typedef unsigned long long int ull;
#define IOS ios_base::sync_with_stdio(0);  cin.tie(0); cout.tie(0);

typedef pair<int,int>pr;
#define mp   make_pair
#define all(i)     i.begin() , i.end()
#define ft     first
#define sn     second
#define pb push_back
#define eps 1e-9
#define SP cout<<fixed<<setprecision(10);

#define inf 100000
#define MAXN 200010

const int mod = 1e9+7;

#define dbg cout<<"rony\n";
#define en "\n"

vector<int>g[MAXN];
int vis[MAXN];

void dfs(int nd)
{
    vis[nd]  = 1;
    for(auto i:g[nd])
    {
        if(vis[i] == 1) continue;
        dfs(i);
    }
}
void solve()
{
  int n,m;
  cin >> n >> m;
  map<int,int>ma;
  int cnt = 1;
  
  for(int i = 1;i <= m;i++)
  {
     int x,y;
     cin >> x >> y;
     if(ma[x] == 0)
     {
        ma[x] = cnt;
        cnt++;
     }
     if(ma[y] == 0)
     {
        ma[y] = cnt;
        cnt++;
     }

     g[ma[x]].pb(ma[y]);
     g[ma[y]].pb(ma[x]);
  }

  int an = n - cnt + 1;
   
  for(int i = 1;i < cnt;i++)
  {
     if(vis[i] == 0)
     {
        an++;
        dfs(i);
     }
  }
  cout<<an<<en;
 
}

int main()
{
    IOS;
    int t;
  //cin >> t;
  t = 1;
    while ( t-- )
    {

        solve();
    }
    return 0;
}
