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 <vector>

typedef long long ll;

const int MaxN = 100005;

int n, m;
std::vector<std::pair<int, bool>> G[MaxN];

int col[MaxN];
int num[3], deg; bool flag;

void dfs(int u, int c) {
	col[u] = c;
	num[c] += 1;
	deg += (int)G[u].size();
	for (auto e: G[u]) {
		int v = e.first;
		bool rev = e.second;
		int c_v = (col[u] + (rev ? 2 : 1)) % 3;
		if (col[v] == -1) {
			dfs(v, c_v);
		} else if (col[v] != c_v) {
			flag = true;
		}
	}
}

ll solve(int u) {
	std::fill(num, num + 3, 0);
	deg = 0;
	flag = false;
	dfs(u, 0);
	if (flag) {
		int tot = num[0] + num[1] + num[2];
		return (ll)tot * tot;
	} else if (num[1] == 0 || num[2] == 0) {
		return deg / 2;
	} else {
		return (ll)num[0] * num[1] + (ll)num[1] * num[2] + (ll)num[2] * num[0];
	}
}

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 0; i < m; ++i) {
		int a, b;
		scanf("%d%d", &a, &b);
		G[a].push_back({b, 0});
		G[b].push_back({a, 1});
	}
	std::fill(col + 1, col + n + 1, -1);
	ll ans = 0;
	for (int i = 1; i <= n; ++i) {
		if (col[i] == -1) ans += solve(i);
	}
	printf("%lld\n", ans);
	return 0;
}