#include #include #include #include using namespace std; using namespace atcoder; using ll = long long; int main() { ll n, m; cin >> n >> m; vector in(n), out(n); dsu d(n); for (ll i = 0; i < m; i++) { ll u, v; cin >> u >> v; u--; v--; out[u]++; in[v]++; d.merge(u, v); } unordered_map ind; for (ll i = 0; i < n; i++) ind[d.leader(i)] += max(0LL, in[i] - out[i]); ll ans = 0; for (auto& [k, v] : ind) { ans += max(1LL, v); } cout << max(0LL, ans - 1) << "\n"; }