#include using namespace std; using ll = long long; struct UnionFind { int ngroup, N; vector par; vector siz; UnionFind(int _n) : par(_n), siz(_n), N(_n) { ngroup = _n; for(int i = 0; i < N; i++){ par[i] = i; siz[i] = 1; } } int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } int unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx == ry) return rx; ngroup--; if (siz[rx] > siz[ry]) swap(rx, ry); par[rx] = ry; siz[ry] += siz[rx]; return ry; } bool same(int x, int y) { int rx = root(x); int ry = root(y); return rx == ry; } int size(int x){ return siz[root(x)]; } int group_count(){ return ngroup; } vector> groups(){ vector rev(N); int nrt=0; for (int i=0; i> res(nrt); for (int i=0; i> N >> M; UnionFind tree(N+1); vector in(N+1), out(N+1); for (int i=0; i> x >> y; tree.unite(x, y); out[x]++; in[y]++; } vector> g = tree.groups(); for (auto &x : g){ bool f=0; cnt = 0; for (auto y : x){ if (in[y] != 0) f=1; cnt += abs(in[y]-out[y]); } ans += max(0LL, cnt/2-1) + f; } cout << ans-1 << endl; return 0; }