





class 						_A:


				"""simple docstring"""


				def __init__(      self					):
									# numbermap keys are the number and its values are lists of indexes sorted
									# in ascending order
									__A     :						dict[int, list[int]]          =    {}
									# indexmap keys are an index and it's values are the number at that index
									__A     :						dict[int, int]          =    {}



				def  UpperCAmelCase_		(      self					, _A					, _A					):
									if isinstance(_A					, (range, str)					):
														__A     :						Tuple          =    list(_A					)
									elif not isinstance(_A					, _A					):
														raise TypeError(
														    'binary_search_delete() only accepts either a list, range or str'					)

									__A     :						Optional[int]          =    0
									__A     :						int          =    len(_A					) - 1

									while low <= high:
														__A     :						List[str]          =    (low + high) // 2
														if array[mid] == item:
																			array.pop(_A					)
																			return array
														elif array[mid] < item:
																			__A     :						Union[str, Any]          =    mid + 1
														else:
																			__A     :						str          =    mid - 1
									raise ValueError(
									    'Either the item is not in the array or the array was unsorted'					)



				def  UpperCAmelCase_		(      self					, _A					, _A					):
									if isinstance(_A					, (range, str)					):
														__A     :						int          =    list(_A					)
									elif not isinstance(_A					, _A					):
														raise TypeError(
														    'binary_search_insert() only accepts either a list, range or str'					)

									__A     :						List[str]          =    0
									__A     :						int          =    len(_A					) - 1

									while low <= high:
														__A     :						List[Any]          =    (low + high) // 2
														if array[mid] == index:
																			# If the item already exists in the array,
																			# insert it after the existing item
																			array.insert(mid + 1					, _A					)
																			return array
														elif array[mid] < index:
																			__A     :						Dict          =    mid + 1
														else:
																			__A     :						Any          =    mid - 1

        # If the item doesn't exist in the array, insert it at the appropriate position
									array.insert(_A					, _A					)
									return array



				def  UpperCAmelCase_		(      self					, _A					, _A					):
									# Remove previous index
									if index in self.indexmap:
														__A     :						Any          =    self.indexmap[index]
														if len(self.numbermap[n]					) == 1:
																			del self.numbermap[n]
														else:
																			__A     :						Optional[int]          =    self.binary_search_delete(self.numbermap[n]					, _A					)

        # Set new index
									__A     :						str          =    number

									# Number not seen before or empty so insert number value
									if number not in self.numbermap:
														__A     :						Dict          =    [index]

									# Here we need to perform a binary search insertion in order to insert
									# The item in the correct place
									else:
														__A     :						str          =    self.binary_search_insert(
														    self.numbermap[number]					, _A					)





				def  UpperCAmelCase_		(      self					, _A					):
									return self.numbermap.get(_A					, [-1]					)[0]


if __name__ == "__main__":
				import doctest

				doctest.testmod()











from __future__ import annotations







def 			_SCREAMING_SNAKE_CASE    (       a  )				->				List[str]:
					global graph, reversed_graph, scc, component, visit, stack
					if visit[u]:
										return
					__A     :						List[Any]          =    True
					for v in graph[u]:
										dfs(a  )
					stack.append(a  )







def 			_SCREAMING_SNAKE_CASE    (       a  )				->				Optional[Any]:
					global graph, reversed_graph, scc, component, visit, stack
					if visit[u]:
										return
					__A     :						str          =    True
					component.append(a  )
					for v in reversed_graph[u]:
										dfsa(a  )







def 			_SCREAMING_SNAKE_CASE    (       )				->				Optional[int]:
					global graph, reversed_graph, scc, component, visit, stack
					for i in range(a  ):
										dfs(a  )
					__A     :						str          =    [False] * n
					for i in stack[::-1]:
										if visit[i]:
															continue
										__A     :						Optional[Any]          =    []
										dfsa(a  )
										scc.append(a  )
					return scc


if __name__ == "__main__":
				# n - no of nodes, m - no of edges
				UpperCAmelCase  ,			UpperCAmelCase						:       List[str]        =					list(map(int, input().strip().split()))

				UpperCAmelCase						:       list[list[int]]        =					[[] for _ in range(n)]  # graph
				UpperCAmelCase						:       list[list[int]]        =					[[] for i in range(n)]  # reversed graph
				# input graph data (edges)
				for _ in range(m):
								UpperCAmelCase  ,			UpperCAmelCase						:       int        =					list(map(int, input().strip().split()))
								graph[u].append(v)
								reversed_graph[v].append(u)

				UpperCAmelCase						:       list[int]        =					[]
				UpperCAmelCase						:       list[bool]        =					[False] * n
				UpperCAmelCase						:       list[int]        =					[]
				UpperCAmelCase						:       list[int]        =					[]
				print(kosaraju())




