#!/usr/bin/env python3 import sys sys.setrecursionlimit(int(2.1e5)) # Count the number of leaf SCCs using Tarjans algorithm. N, m = map(int, input().split()) g = [[] for _ in range(N)] for _ in range(m): a, b = map(lambda x: int(x) - 1, input().split()) g[b].append(a) counter = 0 n = [-1] * N l = [-1] * N vs = [False] * N st = [] ans = 0 com = [-1] * N C = 0 def visit(u): global counter, ans, C, st, n, l l[u] = n[u] = counter st.append(u) counter += 1 vs[u] = True for v in g[u]: if n[v] == -1: visit(v) if vs[v]: l[u] = min(l[u], l[v]) if l[u] == n[u]: cc = [] while True: v = st[-1] st.pop() vs[v] = False com[v] = C cc.append(v) if u == v: break leaf = True for v in cc: for w in g[v]: if com[w] != C: leaf = False if leaf: ans += 1 C += 1 for u in range(N): if n[u] == -1: visit(u) print(ans)