import itertools as iter import collections as coll import heapq as hq import bisect as bis from decimal import Decimal as dec from copy import deepcopy as dcopy import math import sys sys.setrecursionlimit(10**6) def input(): return sys.stdin.readline().rstrip() def getN(): return int(sys.stdin.readline()) def getNs(): return map(int,sys.stdin.readline().split()) def getList(): return list(map(int,sys.stdin.readline().split())) def strinps(n): return [sys.stdin.readline().rstrip() for _ in range(n)] pi = 3.141592653589793 mod = 10**9+7 MOD = 998244353 INF = math.inf dx = [1,0,-1,0]; dy = [0,1,0,-1] """ 強連結成分分解(SCC) from : https://github.com/ryusuke920/kyopro_educational_90_python/blob/main/solve_python/021.py ref : https://manabitimes.jp/math/1250 """ class SCC: def __init__(self, n): self.n = n self.graph = [[] for _ in range(n)] self.rev_graph = [[] for _ in range(n)] self.labels = [-1] * n self.lb_cnt = 0 #グラフに有向辺を追加(逆向きのものを含む) def add_edge(self, v, nxt_v): self.graph[v].append(nxt_v) self.rev_graph[nxt_v].append(v) #SCCグラフの構成 def build(self): self.post_order = [] self.used = [False] * self.n for v in range(self.n): if(not self.used[v]): self._dfs(v) for v in reversed(self.post_order): if(self.labels[v] == -1): self._rev_dfs(v) self.lb_cnt += 1 #SCCグラフを作る上でのDFS def _dfs(self, v): stack = [v, 0] while(stack): v, idx = stack[-2:] if not idx and self.used[v]: stack.pop() stack.pop() else: self.used[v] = True if idx < len(self.graph[v]): stack[-1] += 1 stack.append(self.graph[v][idx]) stack.append(0) else: stack.pop() self.post_order.append(stack.pop()) #逆向きDFS def _rev_dfs(self, v): stack = [v] self.labels[v] = self.lb_cnt while stack: v = stack.pop() for nxt_v in self.rev_graph[v]: if self.labels[nxt_v] != -1: continue stack.append(nxt_v) self.labels[nxt_v] = self.lb_cnt #構成し終えたSCCグラフを連結成分ごとにリストにする def construct(self): self.dag = [[] for i in range(self.lb_cnt)] self.groups = [[] for i in range(self.lb_cnt)] for v, lb in enumerate(self.labels): for nxt_v in self.graph[v]: nxt_lb = self.labels[nxt_v] if lb == nxt_lb: continue self.dag[lb].append(nxt_lb) self.groups[lb].append(v) return self.dag, self.groups """ Main Code """ n,m = getNs() route = [[] for _ in [0] * n] scc = SCC(n) for _ in [0] * m: a,b = [i - 1 for i in getNs()] route[a].append(b) scc.add_edge(a,b) scc.build() dag, _ = scc.construct() s = len(dag) if(s == 1): print(0) exit(0) source = [True] * s sink = [True] * s for i in range(s): for j in dag[i]: sink[i] = source[j] = False print(max(sum(source), sum(sink)))