from typing import Any
class SCREAMING_SNAKE_CASE_		:







   def __init__(    self	:	int							):





    """simple docstring"""


    UpperCamelCase      =			[]
    UpperCamelCase      =			0







   def __str__(    self	:	Optional[Any]							):





    """simple docstring"""


    UpperCamelCase      =			"""<""" + str(self.stack							)[1:-1] + """>"""
    return printed







   def 	lowerCamelCase_  (    self	:	Dict      ,      lowerCamelCase_	:	Any							):





    """simple docstring"""


    self.stack.append(A_							)
    UpperCamelCase      =			self.length + 1







   def 	lowerCamelCase_  (    self	:	Any							):





    """simple docstring"""


    self.rotate(1							)
    UpperCamelCase      =			self.stack[self.length - 1]
    UpperCamelCase      =			self.stack[:-1]
    self.rotate(self.length - 1							)
    UpperCamelCase      =			self.length - 1
    return dequeued







   def 	lowerCamelCase_  (    self	:	List[str]      ,      lowerCamelCase_	:	int							):





    """simple docstring"""


    for _ in range(A_							):
     UpperCamelCase      =			self.stack[0]
     UpperCamelCase      =			self.stack[1:]
     self.put(A_							)
     UpperCamelCase      =			self.length - 1







   def 	lowerCamelCase_  (    self	:	Any							):





    """simple docstring"""


    UpperCamelCase      =			self.get()
    self.put(A_							)
    self.rotate(self.length - 1							)
    return front







   def 	lowerCamelCase_  (    self	:	Tuple							):





    """simple docstring"""


    return self.length

from __future__ import annotations





def  lowercase(  UpperCamelCase_					)		->  Dict:







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





def  lowercase(  UpperCamelCase_					)		->  Optional[int]:







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





def  lowercase(  )		->  Any:







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


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

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

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