100 atcoder#AGC006F. [AGC006F] Blackout

[AGC006F] Blackout

Score : 17001700 points

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 \leq x,y,z \leq N, paint the cell (zz, xx) black.

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

Constraints

  • 1N1051 \leq N \leq 10^5
  • 1M1051 \leq M \leq 10^5
  • 1ai,biN1 \leq a_i,b_i \leq 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.

3 2
1 2
2 3
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.
2 2
1 1
1 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.
4 3
1 2
1 3
4 4
3

No white cells can be painted black.