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
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=200005;
int n,m,vis[maxn],flg,cnt;long long ans,a,b,c;
int e,son[maxn],lnk[maxn],nxt[maxn],w[maxn];
inline char nc(){
	static char buf[100000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int read(){
	int ret=0;bool f=0;char ch=nc();
	while(ch>'9'||ch<'0') f^=ch=='-',ch=nc();
	while(ch<='9'&&ch>='0') ret=ret*10+ch-'0',ch=nc();
	return f?-ret:ret;
}
inline void add_e(int x,int y,int z){son[++e]=y;w[e]=z;nxt[e]=lnk[x];lnk[x]=e;}
void DFS(int x,int y){
	if(~vis[x]){flg|=vis[x]!=y;return;}
	vis[x]=y;y==0?a++:y==1?b++:c++;
	for (int i=lnk[x];i;i=nxt[i]) DFS(son[i],(y+3+w[i])%3),cnt+=w[i]>0;
} 
int main(){
	n=read(),m=read();
	for (int i=1,x,y;i<=m;i++)
	x=read(),y=read(),add_e(x,y,1),add_e(y,x,-1);
	memset(vis,255,sizeof vis);
	for (int i=1;i<=n;i++) if(!~vis[i]){
		a=b=c=flg=cnt=0;DFS(i,0);
		if(flg) ans+=(a+b+c)*(a+b+c);
		else if(b&&c) ans+=a*b+b*c+c*a;else ans+=cnt;
	}
	printf("%lld\n",ans);
	return 0;
}