






from __future__ import annotations
def    a__    (	A       ):
   global graph, reversed_graph, scc, component, visit, stack
   if visit[u]:
      return
   lowercase__    :						Union[str, Any]       =			True
   for v in graph[u]:
      dfs(A       )
   stack.append(A       )
def    a__    (	A       ):
   global graph, reversed_graph, scc, component, visit, stack
   if visit[u]:
      return
   lowercase__    :						List[Any]       =			True
   component.append(A       )
   for v in reversed_graph[u]:
      dfsa(A       )
def    a__    (	):
   global graph, reversed_graph, scc, component, visit, stack
   for i in range(A       ):
      dfs(A       )
   lowercase__    :						Optional[Any]       =			[False] * n
   for i in stack[::-1]:
      if visit[i]:
         continue
      lowercase__    :						int       =			[]
      dfsa(A       )
      scc.append(A       )
   return scc


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

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

 a 			=							[]
 a 			=							[False] * n
 a 			=							[]
 a 			=							[]
 print(kosaraju())










__lowerCamelCase						= {0: [1, 2], 1: [0, 3], 2: [0], 3: [1], 4: [5, 6], 5: [4, 6], 6: [4, 5]}

__lowerCamelCase						= {0: [1, 2, 3], 1: [0, 3], 2: [0], 3: [0, 1], 4: [], 5: []}






def    UpperCamelCase__  (   UpperCAmelCase    ,	UpperCAmelCase    ,	UpperCAmelCase     )     ->		list:







							"""simple docstring"""





							_a  :    List[Any]			=						True
							_a  :    Optional[Any]			=						[]

							for neighbour in graph[vert]:
														if not visited[neighbour]:
																					connected_verts += dfs(UpperCAmelCase    ,	UpperCAmelCase    ,	UpperCAmelCase     )

							return [vert, *connected_verts]






def    UpperCamelCase__  (   UpperCAmelCase     )     ->		list:







							"""simple docstring"""





							_a  :    Optional[int]			=						len(UpperCAmelCase     )
							_a  :    Optional[Any]			=						graph_size * [False]
							_a  :    Optional[int]			=						[]

							for i in range(UpperCAmelCase     ):
														if not visited[i]:
																					_a  :    List[str]			=						dfs(UpperCAmelCase    ,	UpperCAmelCase    ,	UpperCAmelCase     )
																					components_list.append(UpperCAmelCase     )

							return components_list


if __name__ == "__main__":
						import doctest

						doctest.testmod()