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
//minamoto
#include<bits/stdc++.h>
#define R register
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
typedef long long ll;
const int N=1e5+5;
struct eg{int v,nx,w;}e[N<<1];int head[N],tot;
inline void add(R int u,R int v,R int w){e[++tot]={v,head[u],w},head[u]=tot;}
int dis[N],c[3],eg,sz,fl,n,m;ll ans;
void dfs(int u,int w){
	dis[u]=w,++c[w],++sz;
	go(u){
		++eg;
		if(dis[v]==-1)dfs(v,(dis[u]+e[i].w)%3);
		else if(dis[v]!=(dis[u]+e[i].w)%3)fl=0;
	}
}
int main(){
	scanf("%d%d",&n,&m);
	for(R int i=1,u,v;i<=m;++i)scanf("%d%d",&u,&v),add(u,v,1),add(v,u,2);
	memset(dis,-1,(n+1)<<2);
	fp(i,1,n)if(dis[i]==-1){
		eg=sz=c[0]=c[1]=c[2]=0,fl=1;
		dfs(i,0);eg>>=1;
		if(fl)ans+=(!c[0]||!c[1]||!c[2]?eg:1ll*c[0]*c[1]+1ll*c[1]*c[2]+1ll*c[2]*c[0]);
		else ans+=1ll*sz*sz;
	}
	printf("%lld\n",ans);
	return 0;
}