PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FW0000028. Hành trình qua các thành phố (Tour)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Hành trình qua các thành phố (Tour)

Tour

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Count ordered pairs (u,v)(u,v) such that vv is reachable from uu in a directed graph, including zero-edge walks u=vu=v.

Input

Integers N,MN,M, followed by MM directed edges Ai,BiA_i,B_i; vertices are 1-indexed.

Output

Print the number of reachable ordered pairs.

Subtasks

  • Subtask 1 (40 points): N≤20N\le20; 2≤N≤20002\le N\le2000, 0≤M≤min⁡(2000,N(N−1))0\le M\le\min(2000,N(N-1)); distinct directed edges and Ai≠BiA_i\ne B_i.
  • Subtask 2 (60 points): 2≤N≤20002\le N\le2000, 0≤M≤min⁡(2000,N(N−1))0\le M\le\min(2000,N(N-1)); distinct directed edges and Ai≠BiA_i\ne B_i.

Examples

Input

3 3
1 2
2 3
3 2

Output

7

Explanation

City 1 can reach 1, 2, 3; city 2 can reach 2, 3; city 3 can reach 3, 2. Thus the total is 3+2+2=73+2+2=7, including the three zero-edge routes.