typedef long long ll; typedef long double ld; #include using namespace std; #define int long long // Union-Find struct UnionFind { // core member vector par; // constructor UnionFind() { } UnionFind(int n) : par(n, -1) { } void init(int n) { par.assign(n, -1); } // core methods int root(int x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } bool same(int x, int y) { return root(x) == root(y); } bool merge(int x, int y) { x = root(x), y = root(y); if (x == y) return false; if (par[x] > par[y]) swap(x, y); // merge technique par[x] += par[y]; par[y] = x; return true; } int size(int x) { return -par[root(x)]; } // debug friend ostream& operator << (ostream &s, UnionFind uf) { map> groups; for (int i = 0; i < uf.par.size(); ++i) { int r = uf.root(i); groups[r].push_back(i); } for (const auto &it : groups) { s << "group: "; for (auto v : it.second) s << v << " "; s << endl; } return s; } }; signed main(){ ll n,m; std::cin >> n>>m; vector nyu(n),shu(n); UnionFind uf(n); vector jiko(n); for (int i = 0; i < m; i++) { ll u,v; std::cin >> u>>v; u--;v--; uf.merge(u,v); shu[u]++; nyu[v]++; if(u==v){ jiko[u]++; } } ll p = 0; set kan; set pr; for (int i = 0; i < n; i++) { if(shu[i]-nyu[i]>0){ p += shu[i]-nyu[i]; pr.insert(uf.root(i)); } if(shu[i]==nyu[i]&&shu[i]>0){ kan.insert(uf.root(i)); } } // std::cout << p << std::endl; std::cout << max(0ll,p-(ll)pr.size())+max(0ll,(ll)kan.size()-1) << std::endl; };