




from __future__ import annotations





def      UpperCAmelCase  (						UpperCamelCase__   )     ->     str:







				'''simple docstring'''


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





def      UpperCAmelCase  (						UpperCamelCase__   )     ->     str:







				'''simple docstring'''


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





def      UpperCAmelCase  (						)     ->     Any:







				'''simple docstring'''


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


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

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

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









def       lowerCAmelCase			(		lowerCAmelCase_			)->			int:
				lowerCAmelCase_			:   Optional[int]     					=					len(lowerCAmelCase_			)
				if n in (0, 1):
								return 0
				return sum(query * (n - i - 1) for i, query in enumerate(sorted(lowerCAmelCase_			)			)			)


if __name__ == "__main__":
						import doctest

						doctest.testmod()