#include using namespace std; using ll = long long; using P = pair; #define rep(i, a, b) for(ll i = a; i < b; ++i) #define rrep(i, a, b) for(ll i = a; i >= b; --i) constexpr ll inf = 4e18; struct SetupIO { SetupIO() { ios::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(30); } } setup_io; struct UnionFind { UnionFind(int N) : n(N), data(N, -1) {} int merge(int a, int b) { assert(0 <= a and a < n); assert(0 <= b and b < n); int x = leader(a), y = leader(b); if(x == y) return x; if(-data[x] < -data[y]) swap(x, y); data[x] += data[y]; data[y] = x; return x; } bool same(int a, int b) { assert(0 <= a and a < n); assert(0 <= b and b < n); return leader(a) == leader(b); } int leader(int a) { assert(0 <= a and a < n); if(data[a] < 0) return a; return data[a] = leader(data[a]); } int size(int a) { assert(0 <= a and a < n); return -data[leader(a)]; } vector> groups() { vector leader_buf(n), group_size(n); for(int i = 0; i < n; ++i) { leader_buf[i] = leader(i); ++group_size[leader_buf[i]]; } vector> result(n); for(int i = 0; i < n; ++i) { result[i].reserve(group_size[i]); } for(int i = 0; i < n; ++i) { result[leader_buf[i]].push_back(i); } result.erase(remove_if(result.begin(), result.end(), [&](const vector& v) { return v.empty(); }), result.end()); return result; } private: int n; vector data; }; int main(void) { int n, m; cin >> n >> m; UnionFind uf(n); vector in_deg(n), out_deg(n); rep(i, 0, m) { int u, v; cin >> u >> v; u--; v--; uf.merge(u, v); in_deg[v]++; out_deg[u]++; } vector> group = uf.groups(); int ans = 0; bool flag = false; for(const vector& v : group) { if(in_deg[v[0]] > 0 or out_deg[v[0]] > 0) { if(flag) { ans++; } else { flag = true; } } int cnt = 0; for(const int x : v) { cnt += abs(in_deg[x] - out_deg[x]); } if(cnt >= 4) ans += (cnt - 2) / 2; } cout << ans << '\n'; }