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>
using namespace std;
#define N 100005
#define LL long long
#define rep(i,j,k) for(i=j;i<=k;++i)
struct E{
	int to,nxt,flag;
}ed[N<<1];
int n,m,i,j,k,top,a,b;
int head[N],vis[N];
bool flag;
LL ans,s[5];
void read(int &p){
	p=0; char x=getchar();
	while(x<'0' || x>'9') x=getchar();
	while(x>='0'&&x<='9'){p=p*10+x-'0'; x=getchar();}
}
void add(int u,int v,int f){
	top++; ed[top]=(E){v,head[u],f}; head[u]=top;
}
void dfs(int x,int c){
	int i,v,nc[2];
	vis[x]=c; s[0]++; s[c]++; nc[1]=(c%3)+1; 
	if(c==1) nc[0]=3;
	else nc[0]=c-1;
	for(i=head[x];i;i=ed[i].nxt){
		v=ed[i].to;
		if(ed[i].flag) s[4]++;
		if(vis[v]){
			if(vis[v]!=nc[ed[i].flag]) flag=false;
		}else dfs(v,nc[ed[i].flag]);
	}
}
int main(){
	read(n); read(m);
	rep(i,1,m){
		read(a); read(b);
		add(a,b,1); add(b,a,0);
	}
	rep(i,1,n)
	if(!vis[i]){
		s[4]=s[0]=s[1]=s[2]=s[3]=0; flag=true;
		dfs(i,1);
		if(flag){
			if((!s[1])||(!s[2])||(!s[3])){
				ans+=(LL)s[4];
			}else{
				ans+=(LL)s[1]*1LL*s[2];
				ans+=(LL)s[2]*1LL*s[3];
				ans+=(LL)s[3]*1LL*s[1];
			}
		}else ans+=(LL)s[0]*1LL*s[0];
	}
	printf("%lld\n",ans);
	return 0;
}