from __future__ import annotations

from collections.abc import Iterable, Iterator
from typing import Any, Generic, TypeVar

lowerCAmelCase__					=				TypeVar('T', bound=bool)
class        a_				(		Generic[T]		):






 '''simple docstring'''






 def __init__(    self      :						Optional[int]    ,					lowercase__      :						T):







        '''simple docstring'''



        lowerCAmelCase__  			=      value
        lowerCAmelCase__  			=      None
        lowerCAmelCase__  			=      None




 @property
 def 	__snake_case		(    self      :						Union[str, Any]):







        '''simple docstring'''



        return self._value




 @staticmethod
 def 	__snake_case		(    lowercase__      :						SkewNode[T] | None    ,					lowercase__      :						SkewNode[T] | None):







        '''simple docstring'''



        if not roota:
               return roota

        if not roota:
               return roota

        if roota.value > roota.value:
               lowerCAmelCase__			,      lowerCAmelCase__  			=      roota, roota

        lowerCAmelCase__  			=      roota
        lowerCAmelCase__  			=      roota.right
        lowerCAmelCase__  			=      roota.left
        lowerCAmelCase__  			=      SkewNode.merge(lowercase__    ,					lowercase__)

        return result






class        a_				(		Generic[T]		):






 '''simple docstring'''






 def __init__(    self      :						List[str]    ,					lowercase__      :						Iterable[T] | None = ()):







        '''simple docstring'''



        lowerCAmelCase__  			=      None
        if data:
               for item in data:
                      self.insert(lowercase__)




 def __bool__(    self      :						int):







        '''simple docstring'''



        return self._root is not None




 def __iter__(    self      :						Any):







        '''simple docstring'''



        lowerCAmelCase__  			=      []
        while self:
               result.append(self.pop())

        # Pushing items back to the heap not to clear it.
        for item in result:
               self.insert(lowercase__)

        return iter(lowercase__)




 def 	__snake_case		(    self      :						Optional[int]    ,					lowercase__      :						T):







        '''simple docstring'''



        lowerCAmelCase__  			=      SkewNode.merge(self._root    ,					SkewNode(lowercase__))




 def 	__snake_case		(    self      :						List[str]):







        '''simple docstring'''



        lowerCAmelCase__  			=      self.top()
        lowerCAmelCase__  			=      (
            SkewNode.merge(self._root.left    ,					self._root.right) if self._root else None
        )

        return result




 def 	__snake_case		(    self      :						int):







        '''simple docstring'''



        if not self._root:
               raise IndexError('Can\'t get top element for the empty heap.')
        return self._root.value




 def 	__snake_case		(    self      :						Union[str, Any]):







        '''simple docstring'''



        lowerCAmelCase__  			=      None


if __name__ == "__main__":
    import doctest

    doctest.testmod()

from __future__ import annotations






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






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






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


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

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

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