#include #include #include using namespace std; using namespace atcoder; #define rep(i, l, n) for (int i = (l); i < (n); i++) #define ite(i, a) for(auto& i : a) #define all(x) x.begin(), x.end() #define lb lower_bound #define ub upper_bound #define lbi(A, x) (ll)(lb(all(A), x) - A.begin()) #define ubi(A, x) (ll)(ub(all(A), x) - A.begin()) using ll = long long; using Pair = pair; template using V = vector; template using VV = V >; const ll INF = 1000000000000000001; const int inf = 1001001001; const ll mod = 1000000007; const ll MOD = 998244353; template inline V getList(int n) { V res(n); rep(i, 0, n) { cin >> res[i]; }return res; } template inline VV getGrid(int m, int n) { VV res(m, V(n)); rep(i, 0, m) { res[i] = getList(n); }return res; } inline V dtois(string& s) { V vec = {}; for (auto& e : s) { vec.push_back(e - '0'); } return vec; } int main(void) { int n, m; cin >> n >> m; VV route(n, V(0)); scc_graph g(n); rep(i, 0, m) { int a, b; cin >> a >> b; a--; b--; route[a].push_back(b); g.add_edge(a, b); } VV scc = g.scc(); if (scc.size() == 1) { cout << 0 << endl; return 0; } V group(n); rep(i, 0, scc.size()) { ite(p, scc[i]) { group[p] = i; } } V source(scc.size(), 1); V sink(scc.size(), 1); rep(i, 0, n) { ite(j, route[i]) { int s = group[i]; int t = group[j]; if (s != t) { sink[s] = source[t] = 0; } } } int s = 0, t = 0; ite(i, source) { s += i; } ite(i, sink) { t += i; } cout << max(s, t) << endl; return 0; }