#include using namespace std; /* thx for Kohenyan (Debug) */ struct UnionFind { vector par, rank; //init size N UnionFind(int N) { par.assign(N, int()), rank.assign(N, int()); for(int i = 0; i < N; i++) { par[i] = i; rank[i] = 1; } } // Find Loot int find(int x) { if(par[x] == x) { return x; }else { return par[x] = find(par[x]); } } // Unite x and y void unite(int x, int y) { x = find(x); y = find(y); if(x == y) return; if(rank[x] < rank[y]) { par[x] = y; rank[y] += rank[x]; }else { par[y] = x; rank[x] += rank[y]; } } // isSametree? bool same(int x, int y) { return find(x) == find(y); } // size of x's set int size(int x) { return rank[find(x)]; } }; int main() { int N, M; cin >> N >> M; vector A(M, 0), B(M, 0); for(int i = 0; i < M; i++) cin >> A[i] >> B[i]; UnionFind UF(2*N); for(int i = 0; i < M; i++) UF.unite(A[i]-1, B[i]-1); int ans(N); vector seen(2*N, false); for(int i = 0; i < 2*N; i++) { if(seen[UF.find(i)]) continue; else { ans -= UF.size(i) / 2; seen[UF.find(i)] = true; } } cout << ans << endl; return 0; }