fun main() { System.setIn(System.`in`.buffered()) val (n, m) = readInts() val g = List(n) { mutableListOf() } val rg = List(n) { mutableListOf() } repeat(m) { val (a, b) = readInts() g[a - 1].add(b - 1) rg[b - 1].add(a - 1) } val topSort = mutableListOf() val used = BooleanArray(n) { false } for (i in 0 until n) { if (!used[i]) { DeepRecursiveFunction { used[it] = true for (v in g[it]) { if (!used[v]) { callRecursive(v) } } topSort.add(it) }(i) } } val color = IntArray(n) { -1 } var cc = 0 for (i in topSort.reversed()) { if (color[i] == -1) { DeepRecursiveFunction { color[it] = cc for (v in rg[it]) { if (color[v] == -1) { callRecursive(v) } } }(i) cc++ } } val cs = BooleanArray(cc) { true } for (v in g.indices) { for (to in g[v]) { if (color[to] != color[v]) { cs[color[to]] = false } } } println(cs.count { it }) } private fun readStrings() = readln().split(" ") private fun readInts() = readStrings().map { it.toInt() }