
'''simple docstring'''







from __future__ import annotations





def 						snake_case__ (lowerCAmelCase_	):
    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 						snake_case__ (lowerCAmelCase_	):
    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 						snake_case__ ():
    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
      lowercase__     :  Tuple    					=			list(map(int, input().strip().split()))

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

      lowercase__     :  list[int]    					=			[]
      lowercase__     :  list[bool]    					=			[False] * n
      lowercase__     :  list[int]    					=			[]
      lowercase__     :  list[int]    					=			[]
      print(kosaraju())









'''simple docstring'''







import heapq





def 						snake_case__ (lowerCAmelCase_					,  lowerCAmelCase_					,  lowerCAmelCase_	):
    UpperCamelCase__      =			[(0, start)]  # cost from start node,end node
    UpperCamelCase__      =			set()
    while heap:
        ((UpperCamelCase__)    ,  (UpperCamelCase__))      =			heapq.heappop(lowerCAmelCase_	)
        if u in visited:
            continue
        visited.add(lowerCAmelCase_	)
        if u == end:
            return cost
        for v, c in graph[u]:
            if v in visited:
                continue
            UpperCamelCase__      =			cost + c
            heapq.heappush(lowerCAmelCase_					,  (next_item, v)	)
    return -1


lowercase__     :  Any    					=			{
    'A': [['B', 2], ['C', 5]],
    'B': [['A', 2], ['D', 3], ['E', 1], ['F', 1]],
    'C': [['A', 5], ['F', 3]],
    'D': [['B', 3]],
    'E': [['B', 4], ['F', 3]],
    'F': [['C', 3], ['E', 3]],
}
lowercase__     :  Optional[int]    					=			{
    'B': [['C', 1]],
    'C': [['D', 1]],
    'D': [['F', 1]],
    'E': [['B', 1], ['F', 3]],
    'F': [],
}
lowercase__     :  Union[str, Any]    					=			{
    'B': [['C', 1]],
    'C': [['D', 1]],
    'D': [['F', 1]],
    'E': [['B', 1], ['G', 2]],
    'F': [],
    'G': [['F', 1]],
}

lowercase__     :  Union[str, Any]    					=			dijkstra(G, 'E', 'C')
print(short_distance)  # E -- 3 --> F -- 3 --> C == 6

lowercase__     :  Optional[int]    					=			dijkstra(Ga, 'E', 'F')
print(short_distance)  # E -- 3 --> F == 3

lowercase__     :  Any    					=			dijkstra(Ga, 'E', 'F')
print(short_distance)  # E -- 2 --> G -- 1 --> F == 3

if __name__ == "__main__":
      import doctest

      doctest.testmod()




