using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[] NMi => ReadLine().Split().Select(c => int.Parse(c) - 1).ToArray(); static int[][] NMap(int n) => Enumerable.Repeat(0, n).Select(_ => NMi).ToArray(); public static void Main() { Solve(); } static void Solve() { var c = NList; var (n, m) = (c[0], c[1]); var map = NMap(m); var g = new SCCGraph(n); foreach (var edge in map) g.AddEdge(edge[0], edge[1]); var scc = g.SCC(); if (scc.Length == 1) { WriteLine(0); return; } var group = new int[n]; for (var i = 0; i < scc.Length; ++i) { foreach (var pos in scc[i]) group[pos] = i; } var osc = new int[scc.Length]; var isc = new int[scc.Length]; foreach (var edge in map) { if (group[edge[0]] != group[edge[1]]) { ++osc[group[edge[0]]]; ++isc[group[edge[1]]]; } } WriteLine(Math.Max(osc.Count(i => i == 0), isc.Count(i => i == 0))); } class SCCGraph { private int _n; private List<(int from, int to)> edges; public SCCGraph(int n) { _n = n; edges = new List<(int, int)>(); } public int NumVertices() { return _n; } public void AddEdge(int from, int to) { edges.Add((from, to )); } public (int, int[]) SCCIds() { var g = new CSR(_n, edges); var nowOrd = 0; var groupNum = 0; var visited = new List(_n); var low = new int[_n]; var ord = Enumerable.Repeat(-1, _n).ToArray(); var ids = new int[_n]; void DFS(int v) { low[v] = ord[v] = nowOrd++; visited.Add(v); for (var i = g.Start[v]; i < g.Start[v + 1]; ++i) { var to = g.EList[i]; if (ord[to] == -1) { DFS(to); low[v] = Math.Min(low[v], low[to]); } else low[v] = Math.Min(low[v], ord[to]); } if (low[v] == ord[v]) { while (true) { var u = visited.Last(); visited.RemoveAt(visited.Count - 1); ord[u] = _n; ids[u] = groupNum; if (u == v) break; } ++groupNum; } } for (var i = 0; i < _n; ++i) if (ord[i] == -1) DFS(i); for (var i = 0; i < ids.Length; ++i) ids[i] = groupNum - 1 - ids[i]; return (groupNum, ids); } public List[] SCC() { var (groupNum, list) = SCCIds(); var counts = new int[groupNum]; foreach (var x in list) ++counts[x]; var groups = new List[groupNum]; for (var i = 0; i < groups.Length; ++i) groups[i] = new List(counts[i]); for (var i = 0; i < _n; ++i) groups[list[i]].Add(i); return groups; } } // 有向辺のリスト class CSR { public int[] Start { get; private set; } public E[] EList { get; private set; } public CSR(int n, List<(int, E)> edges) { Start = new int[n + 1]; EList = new E[edges.Count]; foreach (var e in edges) ++Start[e.Item1 + 1]; for (var i = 1; i <= n; ++i) Start[i] += Start[i - 1]; var counter = (int[])Start.Clone(); foreach (var e in edges) { EList[counter[e.Item1]++] = e.Item2; } } } }