from __future__ import annotations

import unittest

from transformers import MPNetConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
     import tensorflow as tf

     from transformers.models.mpnet.modeling_tf_mpnet import (
         TFMPNetForMaskedLM,
         TFMPNetForMultipleChoice,
         TFMPNetForQuestionAnswering,
         TFMPNetForSequenceClassification,
         TFMPNetForTokenClassification,
         TFMPNetModel,
     )







class 			SCREAMING_SNAKE_CASE__ :

       def __init__(				self :			str       ,       _lowerCAmelCase :			str       ,       _lowerCAmelCase :			List[str]=13       ,       _lowerCAmelCase :			Any=7       ,       _lowerCAmelCase :			Any=True       ,       _lowerCAmelCase :			List[Any]=True       ,       _lowerCAmelCase :			int=False       ,       _lowerCAmelCase :			Any=True       ,       _lowerCAmelCase :			List[str]=99       ,       _lowerCAmelCase :			int=64       ,       _lowerCAmelCase :			Union[str, Any]=2       ,       _lowerCAmelCase :			int=4       ,       _lowerCAmelCase :			Dict=64       ,       _lowerCAmelCase :			List[Any]="gelu"       ,       _lowerCAmelCase :			Tuple=0.1       ,       _lowerCAmelCase :			List[str]=0.1       ,       _lowerCAmelCase :			Dict=5_12       ,       _lowerCAmelCase :			Dict=16       ,       _lowerCAmelCase :			Optional[int]=2       ,       _lowerCAmelCase :			List[Any]=0.02       ,       _lowerCAmelCase :			str=3       ,       _lowerCAmelCase :			Optional[int]=4       ,       _lowerCAmelCase :			List[str]=None       ,       ):
           __snake_case    :							Optional[int]             =  parent
           __snake_case    :							List[Any]             =  batch_size
           __snake_case    :							str             =  seq_length
           __snake_case    :							List[str]             =  is_training
           __snake_case    :							str             =  use_input_mask
           __snake_case    :							List[str]             =  use_token_type_ids
           __snake_case    :							Tuple             =  use_labels
           __snake_case    :							Dict             =  vocab_size
           __snake_case    :							Any             =  hidden_size
           __snake_case    :							Any             =  num_hidden_layers
           __snake_case    :							Tuple             =  num_attention_heads
           __snake_case    :							List[Any]             =  intermediate_size
           __snake_case    :							Dict             =  hidden_act
           __snake_case    :							List[Any]             =  hidden_dropout_prob
           __snake_case    :							List[str]             =  attention_probs_dropout_prob
           __snake_case    :							str             =  max_position_embeddings
           __snake_case    :							Dict             =  type_vocab_size
           __snake_case    :							Tuple             =  type_sequence_label_size
           __snake_case    :							Dict             =  initializer_range
           __snake_case    :							int             =  num_labels
           __snake_case    :							Tuple             =  num_choices
           __snake_case    :							Optional[Any]             =  scope

       def        snake_case__  (				self :			List[str]       ):
           __snake_case    :							str             =  ids_tensor([self.batch_size, self.seq_length]       ,       self.vocab_size       )

           __snake_case    :							Tuple             =  None
           if self.use_input_mask:
               __snake_case    :							Any             =  random_attention_mask([self.batch_size, self.seq_length]       )

           __snake_case    :							List[str]             =  None
           __snake_case    :							Optional[int]             =  None
           __snake_case    :							Dict             =  None
           if self.use_labels:
               __snake_case    :							List[str]             =  ids_tensor([self.batch_size]       ,       self.type_sequence_label_size       )
               __snake_case    :							List[Any]             =  ids_tensor([self.batch_size, self.seq_length]       ,       self.num_labels       )
               __snake_case    :							int             =  ids_tensor([self.batch_size]       ,       self.num_choices       )

           __snake_case    :							str             =  MPNetConfig(
               vocab_size=self.vocab_size       ,       hidden_size=self.hidden_size       ,       num_hidden_layers=self.num_hidden_layers       ,       num_attention_heads=self.num_attention_heads       ,       intermediate_size=self.intermediate_size       ,       hidden_act=self.hidden_act       ,       hidden_dropout_prob=self.hidden_dropout_prob       ,       attention_probs_dropout_prob=self.attention_probs_dropout_prob       ,       max_position_embeddings=self.max_position_embeddings       ,       initializer_range=self.initializer_range       ,       )
           return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels

       def        snake_case__  (				self :			int       ,       _lowerCAmelCase :			Optional[Any]       ,       _lowerCAmelCase :			str       ,       _lowerCAmelCase :			Optional[int]       ,       _lowerCAmelCase :			List[str]       ,       _lowerCAmelCase :			Tuple       ,       _lowerCAmelCase :			str       ):
           __snake_case    :							Dict             =  TFMPNetModel(config=lowercase_       )
           __snake_case    :							Optional[int]             =  {"""input_ids""": input_ids, """attention_mask""": input_mask}
           __snake_case    :							Optional[int]             =  model(lowercase_       )
           __snake_case    :							List[str]             =  [input_ids, input_mask]
           __snake_case    :							Union[str, Any]             =  model(lowercase_       )
           self.parent.assertEqual(result.last_hidden_state.shape       ,       (self.batch_size, self.seq_length, self.hidden_size)       )

       def        snake_case__  (				self :			int       ,       _lowerCAmelCase :			Union[str, Any]       ,       _lowerCAmelCase :			str       ,       _lowerCAmelCase :			Tuple       ,       _lowerCAmelCase :			int       ,       _lowerCAmelCase :			Optional[int]       ,       _lowerCAmelCase :			List[str]       ):
           __snake_case    :							Union[str, Any]             =  TFMPNetForMaskedLM(config=lowercase_       )
           __snake_case    :							int             =  {"""input_ids""": input_ids, """attention_mask""": input_mask}
           __snake_case    :							Optional[Any]             =  model(lowercase_       )
           self.parent.assertEqual(result.logits.shape       ,       (self.batch_size, self.seq_length, self.vocab_size)       )

       def        snake_case__  (				self :			Optional[Any]       ,       _lowerCAmelCase :			List[Any]       ,       _lowerCAmelCase :			List[str]       ,       _lowerCAmelCase :			List[str]       ,       _lowerCAmelCase :			List[str]       ,       _lowerCAmelCase :			Tuple       ,       _lowerCAmelCase :			Any       ):
           __snake_case    :							int             =  TFMPNetForQuestionAnswering(config=lowercase_       )
           __snake_case    :							List[str]             =  {
               """input_ids""": input_ids,
               """attention_mask""": input_mask,
           }
           __snake_case    :							Dict             =  model(lowercase_       )
           self.parent.assertEqual(result.start_logits.shape       ,       (self.batch_size, self.seq_length)       )
           self.parent.assertEqual(result.end_logits.shape       ,       (self.batch_size, self.seq_length)       )

       def        snake_case__  (				self :			List[str]       ,       _lowerCAmelCase :			List[str]       ,       _lowerCAmelCase :			str       ,       _lowerCAmelCase :			int       ,       _lowerCAmelCase :			Optional[int]       ,       _lowerCAmelCase :			Optional[int]       ,       _lowerCAmelCase :			List[Any]       ):
           __snake_case    :							Any             =  self.num_labels
           __snake_case    :							Optional[int]             =  TFMPNetForSequenceClassification(lowercase_       )
           __snake_case    :							Tuple             =  {"""input_ids""": input_ids, """attention_mask""": input_mask}
           __snake_case    :							Optional[Any]             =  model(lowercase_       )
           self.parent.assertEqual(result.logits.shape       ,       (self.batch_size, self.num_labels)       )

       def        snake_case__  (				self :			Union[str, Any]       ,       _lowerCAmelCase :			int       ,       _lowerCAmelCase :			Optional[int]       ,       _lowerCAmelCase :			Optional[int]       ,       _lowerCAmelCase :			Optional[int]       ,       _lowerCAmelCase :			List[Any]       ,       _lowerCAmelCase :			Tuple       ):
           __snake_case    :							Dict             =  self.num_choices
           __snake_case    :							int             =  TFMPNetForMultipleChoice(lowercase_       )
           __snake_case    :							List[Any]             =  tf.tile(tf.expand_dims(lowercase_       ,       1       )       ,       (1, self.num_choices, 1)       )
           __snake_case    :							List[str]             =  tf.tile(tf.expand_dims(lowercase_       ,       1       )       ,       (1, self.num_choices, 1)       )
           __snake_case    :							Optional[Any]             =  {
               """input_ids""": multiple_choice_inputs_ids,
               """attention_mask""": multiple_choice_input_mask,
           }
           __snake_case    :							List[str]             =  model(lowercase_       )
           self.parent.assertEqual(result.logits.shape       ,       (self.batch_size, self.num_choices)       )

       def        snake_case__  (				self :			Tuple       ,       _lowerCAmelCase :			Tuple       ,       _lowerCAmelCase :			Dict       ,       _lowerCAmelCase :			Union[str, Any]       ,       _lowerCAmelCase :			int       ,       _lowerCAmelCase :			str       ,       _lowerCAmelCase :			List[Any]       ):
           __snake_case    :							List[str]             =  self.num_labels
           __snake_case    :							Dict             =  TFMPNetForTokenClassification(lowercase_       )
           __snake_case    :							int             =  {"""input_ids""": input_ids, """attention_mask""": input_mask}
           __snake_case    :							Any             =  model(lowercase_       )
           self.parent.assertEqual(result.logits.shape       ,       (self.batch_size, self.seq_length, self.num_labels)       )





       def        snake_case__  (				self :			Dict       ):
           __snake_case    :							Dict             =  self.prepare_config_and_inputs()
           ((__snake_case)       ,      (__snake_case)       ,      (__snake_case)       ,      (__snake_case)       ,      (__snake_case)       ,      (__snake_case))    :							Optional[Any]             =  config_and_inputs
           __snake_case    :							Tuple             =  {"""input_ids""": input_ids, """attention_mask""": input_mask}
           return config, inputs_dict







@require_tf
class 			SCREAMING_SNAKE_CASE__ (   SCREAMING_SNAKE_CASE_					,				SCREAMING_SNAKE_CASE_					,				unittest.TestCase  ):
       A				:     Union[str, Any]					    =     (
           (
               TFMPNetForMaskedLM,
               TFMPNetForMultipleChoice,
               TFMPNetForQuestionAnswering,
               TFMPNetForSequenceClassification,
               TFMPNetForTokenClassification,
               TFMPNetModel,
           )
           if is_tf_available()
           else ()
       )
       A				:     Union[str, Any]					    =     (
           {
               "feature-extraction": TFMPNetModel,
               "fill-mask": TFMPNetForMaskedLM,
               "question-answering": TFMPNetForQuestionAnswering,
               "text-classification": TFMPNetForSequenceClassification,
               "token-classification": TFMPNetForTokenClassification,
               "zero-shot": TFMPNetForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )
       A				:     Dict					    =     False
       A				:     List[Any]					    =     False

       def        snake_case__  (				self :			Dict       ):
           __snake_case    :							str             =  TFMPNetModelTester(self       )
           __snake_case    :							Any             =  ConfigTester(self       ,       config_class=lowercase_       ,       hidden_size=37       )

       def        snake_case__  (				self :			Optional[int]       ):
           self.config_tester.run_common_tests()

       def        snake_case__  (				self :			Any       ):
           __snake_case    :							Optional[int]             =  self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mpnet_model(*lowercase_       )

       def        snake_case__  (				self :			Dict       ):
           __snake_case    :							int             =  self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mpnet_for_masked_lm(*lowercase_       )

       def        snake_case__  (				self :			Optional[Any]       ):
           __snake_case    :							Optional[int]             =  self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mpnet_for_question_answering(*lowercase_       )

       def        snake_case__  (				self :			Optional[int]       ):
           __snake_case    :							Dict             =  self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mpnet_for_sequence_classification(*lowercase_       )

       def        snake_case__  (				self :			List[str]       ):
           __snake_case    :							List[str]             =  self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mpnet_for_multiple_choice(*lowercase_       )

       def        snake_case__  (				self :			str       ):
           __snake_case    :							Optional[Any]             =  self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mpnet_for_token_classification(*lowercase_       )





       @slow
       def        snake_case__  (				self :			Dict       ):
           for model_name in ["microsoft/mpnet-base"]:
               __snake_case    :							Optional[int]             =  TFMPNetModel.from_pretrained(lowercase_       )
               self.assertIsNotNone(lowercase_       )







@require_tf
class 			SCREAMING_SNAKE_CASE__ (   unittest.TestCase  ):

       @slow
       def        snake_case__  (				self :			int       ):
           __snake_case    :							List[str]             =  TFMPNetModel.from_pretrained("""microsoft/mpnet-base"""       )
           __snake_case    :							Any             =  tf.constant([[0, 1, 2, 3, 4, 5]]       )
           __snake_case    :							int             =  model(lowercase_       )[0]

           __snake_case    :							Optional[int]             =  [1, 6, 7_68]
           self.assertEqual(output.shape       ,       lowercase_       )

           __snake_case    :							Dict             =  tf.constant(
               [
                   [
                       [-0.1067172, 0.08216473, 0.0024543],
                       [-0.03465879, 0.8354118, -0.03252288],
                       [-0.06569476, -0.12424111, -0.0494436],
                   ]
               ]       )
           tf.debugging.assert_near(output[:, :3, :3]       ,       lowercase_       ,       atol=1e-4       )





"""simple docstring"""

from __future__ import annotations




def 			lowercase__  (    lowerCAmelCase :    Dict       )       ->					Optional[int]:






    """simple docstring"""




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




def 			lowercase__  (    lowerCAmelCase :    Optional[int]       )       ->					int:






    """simple docstring"""




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




def 			lowercase__  (    )       ->					List[str]:






    """simple docstring"""




    global graph, reversed_graph, scc, component, visit, stack
    for i in range(lowerCAmelCase       ):
        dfs(lowerCAmelCase       )
    UpperCAmelCase  	=    [False] * n
    for i in stack[::-1]:
        if visit[i]:
            continue
        UpperCAmelCase  	=    []
        dfsa(lowerCAmelCase       )
        scc.append(lowerCAmelCase       )
    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())



