#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;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnR5cGVkZWYgbG9uZyBsb25nIGludCBsbDsKdHlwZWRlZiB1bnNpZ25lZCBsb25nIGxvbmcgaW50IHVsbDsKI2RlZmluZSBJT1MgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTsgIGNpbi50aWUoMCk7IGNvdXQudGllKDApOwoKdHlwZWRlZiBwYWlyPGludCxpbnQ+cHI7CiNkZWZpbmUgbXAgICBtYWtlX3BhaXIKI2RlZmluZSBhbGwoaSkgICAgIGkuYmVnaW4oKSAsIGkuZW5kKCkKI2RlZmluZSBmdCAgICAgZmlyc3QKI2RlZmluZSBzbiAgICAgc2Vjb25kCiNkZWZpbmUgcGIgcHVzaF9iYWNrCiNkZWZpbmUgZXBzIDFlLTkKI2RlZmluZSBTUCBjb3V0PDxmaXhlZDw8c2V0cHJlY2lzaW9uKDEwKTsKCiNkZWZpbmUgaW5mIDEwMDAwMAojZGVmaW5lIE1BWE4gMjAwMDEwCgpjb25zdCBpbnQgbW9kID0gMWU5Kzc7CgojZGVmaW5lIGRiZyBjb3V0PDwicm9ueVxuIjsKI2RlZmluZSBlbiAiXG4iCgp2ZWN0b3I8aW50PmdbTUFYTl07CmludCB2aXNbTUFYTl07Cgp2b2lkIGRmcyhpbnQgbmQpCnsKICAgIHZpc1tuZF0gID0gMTsKICAgIGZvcihhdXRvIGk6Z1tuZF0pCiAgICB7CiAgICAgICAgaWYodmlzW2ldID09IDEpIGNvbnRpbnVlOwogICAgICAgIGRmcyhpKTsKICAgIH0KfQp2b2lkIHNvbHZlKCkKewogIGludCBuLG07CiAgY2luID4+IG4gPj4gbTsKICBtYXA8aW50LGludD5tYTsKICBpbnQgY250ID0gMTsKICAKICBmb3IoaW50IGkgPSAxO2kgPD0gbTtpKyspCiAgewogICAgIGludCB4LHk7CiAgICAgY2luID4+IHggPj4geTsKICAgICBpZihtYVt4XSA9PSAwKQogICAgIHsKICAgICAgICBtYVt4XSA9IGNudDsKICAgICAgICBjbnQrKzsKICAgICB9CiAgICAgaWYobWFbeV0gPT0gMCkKICAgICB7CiAgICAgICAgbWFbeV0gPSBjbnQ7CiAgICAgICAgY250Kys7CiAgICAgfQoKICAgICBnW21hW3hdXS5wYihtYVt5XSk7CiAgICAgZ1ttYVt5XV0ucGIobWFbeF0pOwogIH0KCiAgaW50IGFuID0gbiAtIGNudCArIDE7CiAgIAogIGZvcihpbnQgaSA9IDE7aSA8IGNudDtpKyspCiAgewogICAgIGlmKHZpc1tpXSA9PSAwKQogICAgIHsKICAgICAgICBhbisrOwogICAgICAgIGRmcyhpKTsKICAgICB9CiAgfQogIGNvdXQ8PGFuPDxlbjsKIAp9CgppbnQgbWFpbigpCnsKICAgIElPUzsKICAgIGludCB0OwogIC8vY2luID4+IHQ7CiAgdCA9IDE7CiAgICB3aGlsZSAoIHQtLSApCiAgICB7CgogICAgICAgIHNvbHZlKCk7CiAgICB9CiAgICByZXR1cm4gMDsKfQo=