#include <bits/stdc++.h>
using namespace std;
//ACCEPTED CODE
typedef long long int ll;
typedef unsigned long long int ull;
#define MOD 1000000007
#define _FastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
ll par[300005], node, edge;
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+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgovL0FDQ0VQVEVEIENPREUKCnR5cGVkZWYgbG9uZyBsb25nIGludCBsbDsKdHlwZWRlZiB1bnNpZ25lZCBsb25nIGxvbmcgaW50IHVsbDsKCiNkZWZpbmUgTU9EIDEwMDAwMDAwMDcKI2RlZmluZSBfRmFzdElPIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCk7IGNpbi50aWUoMCk7IGNvdXQudGllKDApCgpsbCBwYXJbMzAwMDA1XSwgbm9kZSwgZWRnZTsKbWFwPGxsLCBsbD4gbXA7CgpsbCBmaW5kX3BhcihsbCByKXsKICAgIGlmKHBhcltyXSA9PSByKXsKICAgICAgICByZXR1cm4gcjsKICAgIH0KICAgIGVsc2UgcmV0dXJuIHBhcltyXSA9IGZpbmRfcGFyKHBhcltyXSk7Cn0KCnZvaWQgdW5pb25fZmluZChsbCB4LCBsbCB5KXsKICAgIGxsIHUgPSBmaW5kX3Bhcih4KTsKICAgIGxsIHYgPSBmaW5kX3Bhcih5KTsKCiAgICBpZih1ICE9IHYpewogICAgICAgIHBhclt2XSA9IHU7CiAgICB9Cn0KCgppbnQgbWFpbigpIHsKCiAgICBfRmFzdElPOwoKICAgIHdoaWxlKGNpbiA+PiBub2RlID4+IGVkZ2UpewogICAgICAgIGZvcihpbnQgaSA9IDE7IGkgPD0gMzAwMDA1OyBpKyspewogICAgICAgICAgICBwYXJbaV0gPSBpOwogICAgICAgIH0KICAgICAgICBsbCBjbnQgPSAwOwogICAgICAgIHdoaWxlKGVkZ2UtLSl7CiAgICAgICAgICAgIGxsIHgsIHk7CiAgICAgICAgICAgIGNpbiA+PiB4ID4+IHk7CgogICAgICAgICAgICBpZihtcFt4XSA9PSAwKXsKICAgICAgICAgICAgICAgIG1wW3hdID0gKytjbnQ7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYobXBbeV0gPT0gMCl7CiAgICAgICAgICAgICAgICBtcFt5XSA9ICsrY250OwogICAgICAgICAgICB9CiAgICAgICAgICAgIHVuaW9uX2ZpbmQobXBbeF0sIG1wW3ldKTsKICAgICAgICB9CiAgICAgICAgbGwgYW5zID0gMDsKICAgICAgICBmb3IobGwgaSA9IDE7IGkgPD0gY250OyBpKyspewogICAgICAgICAgICBsbCBwID0gZmluZF9wYXIoaSk7CiAgICAgICAgICAgIGlmKHAgPT0gaSl7CiAgICAgICAgICAgICAgICBhbnMrKzsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBhbnMgKz0gKG5vZGUtY250KTsKICAgICAgICBjb3V0IDw8IGFucyA8PCAgZW5kbDsKICAgICAgICAgbXAuY2xlYXIoKTsKICAgIH0KCgogICAgcmV0dXJuIDA7Cn0=