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 tree = new List[n]; for (var i = 0; i < tree.Length; ++i) tree[i] = new List(); foreach (var edge in map) tree[edge[0]].Add(edge[1]); var starts = new int[n]; var ends = new int[n]; for (var i = 0; i < n; ++i) { while (tree[i].Count > 0) { ++starts[i]; var pos = i; do { var next = tree[pos][tree[pos].Count - 1]; tree[pos].RemoveAt(tree[pos].Count - 1); pos = next; } while (tree[pos].Count > 0); ++ends[pos]; } } for (var i = 0; i < n; ++i) { var rm = Math.Min(starts[i], ends[i]); starts[i] -= rm; } WriteLine(Math.Max(0, starts.Sum() - 1)); } }