import tempfile
import unittest

import numpy as np
import torch

from diffusers import DiffusionPipeline
from diffusers.models.attention_processor import Attention, AttnAddedKVProcessor
class SCREAMING_SNAKE_CASE_		(			unittest.TestCase		):







   def 	lowerCamelCase_  (    self	:	Union[str, Any]      ,      lowerCamelCase_	:	bool = False							):





    """simple docstring"""


    UpperCamelCase      =			10

    if only_cross_attention:
     UpperCamelCase      =			12
    else:
     # when only cross attention is not set, the cross attention dim must be the same as the query dim
     UpperCamelCase      =			query_dim

    return {
        "query_dim": query_dim,
        "cross_attention_dim": cross_attention_dim,
        "heads": 2,
        "dim_head": 4,
        "added_kv_proj_dim": 6,
        "norm_num_groups": 1,
        "only_cross_attention": only_cross_attention,
        "processor": AttnAddedKVProcessor(),
    }







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





    """simple docstring"""


    UpperCamelCase      =			2

    UpperCamelCase      =			torch.rand(lowerCamelCase_      ,      lowerCamelCase_      ,      3      ,      2							)
    UpperCamelCase      =			torch.rand(lowerCamelCase_      ,      4      ,      lowerCamelCase_							)
    UpperCamelCase      =			None

    return {
        "hidden_states": hidden_states,
        "encoder_hidden_states": encoder_hidden_states,
        "attention_mask": attention_mask,
    }







   def 	lowerCamelCase_  (    self	:	List[Any]							):





    """simple docstring"""


    torch.manual_seed(0							)

    UpperCamelCase      =			self.get_constructor_arguments(only_cross_attention=lowerCamelCase_							)
    UpperCamelCase      =			Attention(**lowerCamelCase_							)

    self.assertTrue(attn.to_k is not None							)
    self.assertTrue(attn.to_v is not None							)

    UpperCamelCase      =			self.get_forward_arguments(
        query_dim=constructor_args["""query_dim"""]      ,      added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]							)

    UpperCamelCase      =			attn(**lowerCamelCase_							)

    # only self attention

    torch.manual_seed(0							)

    UpperCamelCase      =			self.get_constructor_arguments(only_cross_attention=lowerCamelCase_							)
    UpperCamelCase      =			Attention(**lowerCamelCase_							)

    self.assertTrue(attn.to_k is None							)
    self.assertTrue(attn.to_v is None							)

    UpperCamelCase      =			self.get_forward_arguments(
        query_dim=constructor_args["""query_dim"""]      ,      added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]							)

    UpperCamelCase      =			attn(**lowerCamelCase_							)

    self.assertTrue((only_cross_attn_out != self_and_cross_attn_out).all()							)





class SCREAMING_SNAKE_CASE_		(			unittest.TestCase		):







   def 	lowerCamelCase_  (    self	:	List[str]							):





    """simple docstring"""


    UpperCamelCase      =			DiffusionPipeline.from_pretrained("""hf-internal-testing/tiny-stable-diffusion-pipe"""      ,      safety_checker=lowerCamelCase_							)

    UpperCamelCase      =			pipe(
        """foo"""      ,      num_inference_steps=2      ,      generator=torch.Generator("""cpu"""							).manual_seed(0							)      ,      output_type="""np"""      ,      ).images

    # the initial conversion succeeds
    UpperCamelCase      =			DiffusionPipeline.from_pretrained(
        """hf-internal-testing/tiny-stable-diffusion-pipe"""      ,      device_map="""sequential"""      ,      safety_checker=lowerCamelCase_							)

    UpperCamelCase      =			pipe(
        """foo"""      ,      num_inference_steps=2      ,      generator=torch.Generator("""cpu"""							).manual_seed(0							)      ,      output_type="""np"""      ,      ).images

    with tempfile.TemporaryDirectory() as tmpdir:
     # save the converted model
     pipe.save_pretrained(lowerCamelCase_							)

     # can also load the converted weights
     UpperCamelCase      =			DiffusionPipeline.from_pretrained(lowerCamelCase_      ,      device_map="""sequential"""      ,      safety_checker=lowerCamelCase_							)

    UpperCamelCase      =			pipe(
        """foo"""      ,      num_inference_steps=2      ,      generator=torch.Generator("""cpu"""							).manual_seed(0							)      ,      output_type="""np"""      ,      ).images

    self.assertTrue(np.allclose(lowerCamelCase_      ,      lowerCamelCase_      ,      atol=1E-5							)							)
    self.assertTrue(np.allclose(lowerCamelCase_      ,      lowerCamelCase_      ,      atol=1E-5							)							)

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())
