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>
using namespace std;
#define MAXN 100010
#define LL long long
int Adj[MAXN],V[MAXN*2],nxt[MAXN*2],W[MAXN*2],c=1;
void AddEdge(int u,int v)
{
	c++;V[c]=v,nxt[c]=Adj[u],W[c]=1;Adj[u]=c;
	c++;V[c]=u,nxt[c]=Adj[v],W[c]=2;Adj[v]=c;
}
int col[MAXN],vis[MAXN],vise[MAXN],cnt[3],flag,cV,E;
LL ans;
void dfs(int u)
{
	cV++;
	cnt[col[u]]++;
	vis[u]=1;
	for(int i=Adj[u];i;i=nxt[i])
	{
		if(vise[i>>1]) continue;
		vise[i>>1]=1;
		E++;
		if(!vis[V[i]])
		{
			col[V[i]]=(col[u]+W[i])%3;
			dfs(V[i]);
		}
		else if(col[V[i]]!=(col[u]+W[i])%3)
			flag=1;
	}
}
int x[MAXN],y[MAXN];
int main()
{
	int n,m,u,v;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&x[i],&y[i]);
		AddEdge(x[i],y[i]);
	}
	for(int i=1;i<=m;i++)
		if(!vise[i])
		{
			flag=cnt[0]=cnt[1]=cnt[2]=cV=E=0;
			dfs(x[i]);
			if(flag) ans+=1LL*cV*cV;
			else if(!cnt[0]||!cnt[1]||!cnt[2]) ans+=E;
			else ans+=1LL*cnt[0]*cnt[1]+1LL*cnt[1]*cnt[2]+1LL*cnt[2]*cnt[0];
		}
	printf("%lld\n",ans);
}