#!/usr/bin/env python3 # DFS from unvisted vertices, marking everything below as non-root. # Stop once reaching an already twice visited vertex. # (Visiting the 2nd time is needed to find possible currently-marked-as-root # vertices that need to be unmarked.) __import__("sys").setrecursionlimit(int(1.1e5)) # Count the number of root SCCs n, m = map(int, input().split()) g, root, vs, cnt = [[] for _ in range(n)], [True] * n, [0] * n, [0] * n [g[a].append(b) for _ in range(m) for a, b in [[int(i) - 1 for i in input().split()]]] def visit(u, r): for v in g[u]: if vs[v] == r or cnt[v] == 2: continue vs[v], root[v], cnt[v] = r, False, cnt[v] + 1 visit(v, r) for u in range(n): if vs[u] == 0: vs[u], cnt[u] = (r := u + 1), cnt[u] + 1 visit(u, r) print(sum(root))