Problem: We have a grid with N rows and N columns. The cell at the i-th row and j-th column is denoted (i, j).

Initially, M of the cells are painted black, and all other cells are white. Specifically, the cells (a_1, b_1), (a_2, b_2), ..., (a_M, b_M) are painted black.

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

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



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

Constraints

* 1≤N≤10^5
* 1≤M≤10^5
* 1≤a_i,b_i≤N
* All pairs (a_i, b_i) are distinct.

Input

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


N M
a_1 b_1
a_2 b_2
:
a_M b_M


Output

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

Examples

Input

3 2
1 2
2 3


Output

3


Input

2 2
1 1
1 2


Output

4


Input

4 3
1 2
1 3
4 4


Output

3

Solution (in CPP):

Solution:
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

const int MAX_N = 100005;
typedef long long i64;

class Edge {
public:
  int nxt, to, dir;
} e[MAX_N << 1];

int head[MAX_N], id[MAX_N], cnt;
void addedge(int u, int v) {
  e[++cnt] = (Edge){head[u], v, 1}, head[u] = cnt;
  e[++cnt] = (Edge){head[v], u, -1}, head[v] = cnt;
}

int N, M, num[3], flag, edge_num;
void dfs(int u) {
  num[id[u]]++;
  static int dfn[MAX_N], tot;
  dfn[u] = ++tot;
  for (int i = head[u]; i; i = e[i].nxt)
    if (id[e[i].to] == -1) {
      id[e[i].to] = (id[u] + e[i].dir + 3) % 3, edge_num++;
      dfs(e[i].to);
    } else {
      if (dfn[e[i].to] > dfn[u]) edge_num++;
      if (id[e[i].to] != (id[u] + e[i].dir + 3) % 3) flag = 0;
    }
}

int main() {
  memset(id, -1, sizeof id);
  
  scanf("%d%d", &N, &M);
  for (int i = 1, a, b; i <= M; ++i) {
    scanf("%d%d", &a, &b);
    addedge(a, b);
  }

  i64 res = 0;
  for (int i = 1; i <= N; ++i)
    if (id[i] == -1) {
      flag = 1, id[i] = 1, edge_num = 0;
      memset(num, 0, sizeof num);
      
      dfs(i);
      if (flag) {
	if (num[0] && num[1] && num[2]) {
	  res += 1LL * num[0] * num[1] + 1LL * num[1] * num[2] + 1LL * num[2] * num[0];
	} else {
	  res += edge_num;
	}
      } else {
	i64 tot = num[0] + num[1] + num[2];
	res += tot * tot;
      }
    }

  printf("%lld\n", res);
  return 0;
}