constint N = 30000; bitset<N + 1> f[maxn], g[maxn];
int in[maxn], tp[maxn]; voidtopsort(){ queue<int> Q; int cnt = 0; for (int i = 1; i <= n; ++i) if (!in[i]) Q.push(i); while (!Q.empty()) { int u = Q.front(); Q.pop(); tp[++cnt] = u; for (auto v : G[u]) if (--in[v] == 0) Q.push(v); } }
cin >> n >> m; for (int i = 1; i <= m; ++i) { int x, y; cin >> x >> y; G[x].push_back(y); ++in[y]; } topsort(); for (int o = n, u = tp[o]; o; u = tp[--o]) for (auto v : G[u]) f[u].set(v), f[u] |= f[u]; for (int o = 1, u = tp[o]; o <= n; u = tp[++o]) for (auto v : G[u]) g[v].set(u), g[v] |= g[u]; int ans = 0; for (int u = 1; u <= n; ++u) for (auto v : G[u]) ans += (f[u] & g[v]).count() > 0; cout << ans << "\n"; return0; }