#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;
}