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;
ll rd()
{
	ll rx=0,fx=1;char c=getchar();
	while(c<48||c>57)
	{
		if(c=='-') fx=-1;
		c=getchar();
	}
	while(c>=48&&c<=57)
	{
		rx=rx*10+c-48;
		c=getchar();
	}
	return rx*fx;
}
const ll N=2e5+50,E=N<<1;
ll n,m,cnt=1,tag,ans=0,tot,num;
ll he[N],col[N],vis[N],h[3];
struct Edge
{
	ll v,nxt;
}e[E];
void eadd(ll u,ll v)
{
	cnt++;e[cnt].v=v;e[cnt].nxt=he[u];he[u]=cnt;
	cnt++;e[cnt].v=u;e[cnt].nxt=he[v];he[v]=cnt;
}
void dfs(ll x)
{
	ll i,v,w;
	vis[x]=1;
	tot++;
	h[col[x]]++;
	for(i=he[x];i;i=e[i].nxt)
	{
		v=e[i].v;
		num++;
		if(i&1ll) w=col[x]-1;
		else w=col[x]+1;
		if(w<0) w+=3;
		if(w>=3) w-=3;
		if(vis[v])
		{
			if(col[v]!=w) tag=0;
		}
		else
		{
			col[v]=w;
			dfs(v);
		}
	}
}
int main()
{
	ll i,u,v;
	n=rd();m=rd();
	for(i=1;i<=m;i++)
	{
		u=rd();v=rd();
		eadd(u,v);
	}
	for(i=1;i<=n;i++)
	{
		if(vis[i]) continue;
		tag=1;tot=0;num=0;
		memset(h,0,sizeof(h));
		dfs(i);
		//printf("U%lld %lld\n",i,tag);
		if(tag==1)
		{
			if(h[0]&&h[1]&&h[2]) ans+=h[0]*h[1]+h[1]*h[2]+h[2]*h[0];
			else ans+=(num>>1);
		}
		else ans+=tot*tot;
	}
	printf("%lld",ans);
	return 0;
}