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 <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = 100005;

inline int gi()
{
	char c = getchar();
	while (c < '0' || c > '9') c = getchar();
	int sum = 0;
	while ('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar();
	return sum;
}

int n, m;
struct edge
{
	int to, next, w;
} e[maxn * 2];
int h[maxn], tot, ecnt, scnt, label;
int vis[maxn], f[3];

inline void add(int u, int v)
{
	e[++tot] = (edge) {v, h[u], 1}; h[u] = tot;
	e[++tot] = (edge) {u, h[v], 2}; h[v] = tot;
}

void dfs(int u, int d)
{
	vis[u] = d; f[d] += 1; ++scnt;
	for (int i = h[u], v; v = e[i].to, i; i = e[i].next) {
		if (e[i].w == 1) ++ecnt;
		if (vis[v] == -1) dfs(v, (d + e[i].w) % 3);
		else if (vis[v] != (vis[u] + e[i].w) % 3) label = 0;
	}
}

int main()
{
	n = gi(); m = gi();
	for (int u, v, i = 1; i <= m; ++i) {
		u = gi(); v = gi();
		add(u, v);
	}

	memset(vis, -1, sizeof(vis));
	ll ans = 0;
	for (int i = 1; i <= n; ++i) {
		if (vis[i] == -1) {
			label = 1; f[0] = f[1] = f[2] = 0; scnt = ecnt = 0; dfs(i, 0);
			if (label) {
				if (!min(f[0], min(f[1], f[2]))) ans += ecnt;
				else ans += (ll)f[0] * f[1] + (ll)f[1] * f[2] + (ll)f[2] * f[0];
			} else ans += (ll)scnt * scnt;
		}
	}

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