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<algorithm>
#include<cstring>
#include<cctype>
#include<cstdio>
#define rep(i,x,y) for(int i=x; i<=y; ++i)
 
using namespace std;
const int N=100005;
bool f[N][3],mp[8][8],vis[N];
int n,m,h[N],cnt,c[8],d[N];
typedef long long LL;
LL ans;
struct edge{int v,w,n;} e[N<<1];
 
int getint()
{
	char ch;
	while(!isdigit(ch=getchar()));
	int x=ch-48;
	while(isdigit(ch=getchar())) x=x*10+ch-48;
	return x;
}
 
void addedge(int u,int v)
{
	e[cnt]=(edge){v,1,h[u]},h[u]=cnt++;
	e[cnt]=(edge){u,-1,h[v]},h[v]=cnt++;
}
 
int get(int x)
{
	return f[x][0]|(f[x][1]<<1)|(f[x][2]<<2);
}
 
void dfs(int x,int tp)
{
	if(!vis[x]) cnt+=d[x],vis[x]=1;
	if(f[x][tp]) return;
	--c[get(x)],f[x][tp]=1,++c[get(x)];
	for(int i=h[x]; i!=-1; i=e[i].n)
		dfs(e[i].v,(tp+e[i].w+3)%3);
}
 
int main()
{
	n=getint(),m=getint();
	memset(h,-1,sizeof(h));
	rep(i,1,m)
	{
		int u=getint(),v=getint();
		addedge(u,v),++d[u],++d[v];
	}
	rep(i,1,7) rep(j,1,7) rep(k,0,2) if(i&(1<<k) && j&(1<<((k+1)%3))) mp[i][j]=1;
	rep(i,1,n) if(!vis[i])
	{
		memset(c,0,sizeof(c));
		cnt=0,dfs(i,0);
		if(!c[7] && (!c[1] || !c[2] || !c[4])) {ans+=cnt/2; continue;}
		rep(j,1,7) if(c[j])
			rep(k,1,7) if(c[k] && mp[j][k])
				ans+=(LL)c[j]*c[k];
	}
	printf("%lld\n",ans);
	return 0;
}