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>
#define INF 0x3f3f3f3f
#define re register
#define LL long long
#define MOD 998244353
#define MAXN 100005
using namespace std;
struct Edge{
	int to,nex,w;Edge(int _=0,int __=0,int ___=0){to=_,nex=__,w=___;}
}e[MAXN<<1];
int n,m,cnt,es,c[5],hd[MAXN],col[MAXN];
LL ans=0;
bool ok,vis[MAXN];
int rd(){
	int x=0,tp=1;char c;c=getchar();
	while(c<'0' || c>'9'){if(c=='-')tp=-1;c=getchar();}
	while(c>='0' && c<='9'){x=(x*10+c-'0');c=getchar();}
	return x*tp;
}
void adde(int x,int y,int z){e[++cnt]=Edge(y,hd[x],z);hd[x]=cnt;}
LL quick_mod(LL a,LL x){LL res=1;while(x){if(x&1)(res*=a)%=MOD;(a*=a)%=MOD,x>>=1;}return res;}
void dfs(int now){
	c[col[now]]++,vis[now]=1;
	for(int i=hd[now];i;i=e[i].nex){
		es+=(e[i].w==1);
		if(!vis[e[i].to])col[e[i].to]=(col[now]+e[i].w+3)%3,dfs(e[i].to);
		else if(col[e[i].to]!=(col[now]+e[i].w+3)%3)ok=0;
	}
}
int main(){
	// freopen("testdata.in","r",stdin);
	// freopen("shit.out","w",stdout);
	int x,y;
	n=rd(),m=rd();
	for(int i=1;i<=m;i++){
		x=rd(),y=rd();
		adde(x,y,1),adde(y,x,-1);
	}
	for(int i=1;i<=n;i++){
		if(vis[i])continue;
		c[0]=c[1]=c[2]=es=0,ok=1;
		dfs(i);
		if(!ok)ans+=1ll*(c[0]+c[1]+c[2])*(c[0]+c[1]+c[2]);
		else if(c[0] && c[1] && c[2])ans+=1ll*c[0]*c[1]+1ll*c[1]*c[2]+1ll*c[0]*c[2];
		else ans+=es;
	}
	printf("%lld\n",ans);
	// printf("%.2lf\n",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}