#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
//WRONG ANSWER
#define MOD 1000000007
#define _FastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
ll node, edge, par[300005];
map<ll, ll> mp;
ll find_par(ll r){
if(par[r] == r){
return r;
}
else return par[r] = find_par(par[r]);
}
void union_find(ll x, ll y){
ll u = find_par(x);
ll v = find_par(y);
if(u != v){
par[v] = u;
}
}
int main() {
_FastIO;
while(cin >> node >> edge){
for(int i = 1; i <= 300005; i++){
par[i] = i;
}
ll cnt = 0;
while(edge--){
ll x, y;
cin >> x >> y;
if(mp[x] == 0){
mp[x] = ++cnt;
}
if(mp[y] == 0){
mp[y] = ++cnt;
}
union_find(mp[x], mp[y]);
}
ll ans = 0;
for(ll i = 1; i <= cnt; i++){
ll p = find_par(i);
if(p == i){
ans++;
}
}
ans += (node-cnt);
cout << ans << endl;
mp.clear();
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIGxvbmcgbG9uZyBpbnQgbGw7CnR5cGVkZWYgdW5zaWduZWQgbG9uZyBsb25nIGludCB1bGw7CgovL1dST05HIEFOU1dFUgoKI2RlZmluZSBNT0QgMTAwMDAwMDAwNwojZGVmaW5lIF9GYXN0SU8gaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTsgY2luLnRpZSgwKTsgY291dC50aWUoMCkKCmxsIG5vZGUsIGVkZ2UsIHBhclszMDAwMDVdOwptYXA8bGwsIGxsPiBtcDsKCmxsIGZpbmRfcGFyKGxsIHIpewogICAgaWYocGFyW3JdID09IHIpewogICAgICAgIHJldHVybiByOwogICAgfQogICAgZWxzZSByZXR1cm4gcGFyW3JdID0gZmluZF9wYXIocGFyW3JdKTsKfQoKdm9pZCB1bmlvbl9maW5kKGxsIHgsIGxsIHkpewogICAgbGwgdSA9IGZpbmRfcGFyKHgpOwogICAgbGwgdiA9IGZpbmRfcGFyKHkpOwoKICAgIGlmKHUgIT0gdil7CiAgICAgICAgcGFyW3ZdID0gdTsKICAgIH0KfQoKCmludCBtYWluKCkgewoKICAgIF9GYXN0SU87CgogICAgd2hpbGUoY2luID4+IG5vZGUgPj4gZWRnZSl7CiAgICAgICAgZm9yKGludCBpID0gMTsgaSA8PSAzMDAwMDU7IGkrKyl7CiAgICAgICAgICAgIHBhcltpXSA9IGk7CiAgICAgICAgfQogICAgICAgIGxsIGNudCA9IDA7CiAgICAgICAgd2hpbGUoZWRnZS0tKXsKICAgICAgICAgICAgbGwgeCwgeTsKICAgICAgICAgICAgY2luID4+IHggPj4geTsKCiAgICAgICAgICAgIGlmKG1wW3hdID09IDApewogICAgICAgICAgICAgICAgbXBbeF0gPSArK2NudDsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZihtcFt5XSA9PSAwKXsKICAgICAgICAgICAgICAgIG1wW3ldID0gKytjbnQ7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgdW5pb25fZmluZChtcFt4XSwgbXBbeV0pOwogICAgICAgIH0KICAgICAgICBsbCBhbnMgPSAwOwogICAgICAgIGZvcihsbCBpID0gMTsgaSA8PSBjbnQ7IGkrKyl7CiAgICAgICAgICAgIGxsIHAgPSBmaW5kX3BhcihpKTsKICAgICAgICAgICAgaWYocCA9PSBpKXsKICAgICAgICAgICAgICAgIGFucysrOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGFucyArPSAobm9kZS1jbnQpOwogICAgICAgIGNvdXQgPDwgYW5zIDw8ICBlbmRsOwogICAgICAgICBtcC5jbGVhcigpOwogICAgfQoKCiAgICByZXR1cm4gMDsKfQ==