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<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>

using namespace std;

const int MX=100111;
typedef long long LL;

int n,m,c[MX];
struct Edge{
	int t,w,nxt;
	Edge(int _t=0,int _w=0,int _nxt=0):t(_t),w(_w),nxt(_nxt){}
}e[MX<<1];
int ec,hed[MX];

inline void ade(int f,int t){
	e[++ec]=Edge(t,1,hed[f]),hed[f]=ec;
	e[++ec]=Edge(f,-1,hed[t]),hed[t]=ec;
}
int stk[MX],stc,E;
bool dfs(int k,int _c){
	bool r=false;
	c[k]=_c;stk[++stc]=k;
	for(int i=hed[k];i;i=e[i].nxt){
		E+=(e[i].w==1);
		if(c[e[i].t]==-1)r=dfs(e[i].t,(_c+e[i].w+3)%3)||r;
		else r=r||(c[e[i].t]!=(_c+e[i].w+3)%3);
	}
	return r;
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++){
		static int a,b;
		scanf("%d%d",&a,&b);
		ade(a,b);
	}
	memset(c+1,-1,sizeof(int)*n);
	LL ans=0LL;
	for(int i=1;i<=n;i++)if(c[i]==-1){
		stc=0;E=0;
		bool flg=dfs(i,0);
		if(flg)ans+=(LL)stc*stc;
		else{
			int C[3]={0};
			for(int &i=stc;i;i--)C[c[stk[i]]]++;
			if(!C[0]||!C[1]||!C[2])ans+=E;
			else ans+=(LL)C[0]*C[1]+(LL)C[1]*C[2]+(LL)C[2]*C[0];
		}
	}
	printf("%lld\n",ans);
	return 0;
}