
from __future__ import annotations


def  A			(   lowercase					)  ->    Union[str, Any]:
		'''simple docstring'''

		global graph, reversed_graph, scc, component, visit, stack
		if visit[u]:
				return
		UpperCamelCase            =	True
		for v in graph[u]:
				dfs(lowercase					)
		stack.append(lowercase					)


def  A			(   lowercase					)  ->    List[Any]:
		'''simple docstring'''

		global graph, reversed_graph, scc, component, visit, stack
		if visit[u]:
				return
		UpperCamelCase            =	True
		component.append(lowercase					)
		for v in reversed_graph[u]:
				dfsa(lowercase					)


def  A			(   )  ->    str:
		'''simple docstring'''

		global graph, reversed_graph, scc, component, visit, stack
		for i in range(lowercase					):
				dfs(lowercase					)
		UpperCamelCase            =	[False] * n
		for i in stack[::-1]:
				if visit[i]:
						continue
				UpperCamelCase            =	[]
				dfsa(lowercase					)
				scc.append(lowercase					)
		return scc


if __name__ == "__main__":
					# n - no of nodes, m - no of edges
					_UpperCAmelCase   :					Any    =			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   :					str    =			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())


from __future__ import annotations






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






def     lowerCamelCase__				(   a ) ->  List[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     lowerCamelCase__				(   ) ->  str:
   global graph, reversed_graph, scc, component, visit, stack
   for i in range(a ):
      dfs(a )
   _A:    Optional[Any]						     =			[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__						:	Any	     = 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__						:	str	     = 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())



