#agc006f. [agc006_f]Blackout

[agc006_f]Blackout

Problem Statement

We have a grid with NN rows and NN columns. The cell at the ii-th row and jj-th column is denoted (ii, jj).

Initially, MM of the cells are painted black, and all other cells are white. Specifically, the cells (a1a_1, b1b_1), (a2a_2, b2b_2), ......, (aMa_M, bMb_M) are painted black.

Snuke will try to paint as many white cells black as possible, according to the following rule:

  • If two cells (xx, yy) and (yy, zz) are both black and a cell (zz, xx) is white for integers 1x,y,zN1≤x,y,z≤N, paint the cell (zz, xx) black.

Find the number of black cells when no more white cells can be painted black.

Constraints

  • 1N1051≤N≤10^5
  • 1M1051≤M≤10^5
  • 1ai,biN1≤a_i,b_i≤N
  • All pairs (aia_i, bib_i) are distinct.

Input

The input is given from Standard Input in the following format:

NN MM a1a_1 b1b_1 a2a_2 b2b_2 :: aMa_M bMb_M

Output

Print the number of black cells when no more white cells can be painted black.


Sample Input 1

3 2
1 2
2 3

Sample Output 1

3

It is possible to paint one white cell black, as follows:

  • Since cells (11, 22) and (22, 33) are both black and a cell (33, 11) is white, paint the cell (33, 11) black.

Sample Input 2

2 2
1 1
1 2

Sample Output 2

4

It is possible to paint two white cells black, as follows:

  • Since cells (11, 11) and (11, 22) are both black and a cell (22, 11) is white, paint the cell (22, 11) black.
  • Since cells (22, 11) and (11, 22) are both black and a cell (22, 22) is white, paint the cell (22, 22) black.

Sample Input 3

4 3
1 2
1 3
4 4

Sample Output 3

3

No white cells can be painted black.