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 <iostream>
#include <cstdio>
#include <cstring>
#define il inline
#define ri register
#define ll long long
#define Size 200050
using namespace std;
struct point{
	int next,to,w;
}ar[Size];
int head[Size],art,a[Size],
	fa[Size],in[Size],a1[Size],a1t,
	out[Size],sum;
il bool dfs(int);
il void read(int&),link(int,int,int);
int main(){
	ll ans(0);int n,m;read(n),read(m);
	for(int i(1),u,v;i<=m;++i)
		read(u),read(v),link(u,v,1),
			link(v,u,2),++in[v],++out[u];
	memset(a,-1,sizeof(a));
	for(int i(1),j;i<=n;++i){
		if(~a[i])continue;
		a1t=sum=a[i]=0;
		bool pd(dfs(i)),pd1(false);
		for(j=1;j<=a1t;++j)
			if(in[a1[j]]&&out[a1[j]])
			{pd1=true;break;}sum>>=1;
		if(pd1){
			if(pd)ans+=(ll)a1t*a1t;
			else{
				int a2[3]{};for(j=1;j<=a1t;++j)++a2[a[a1[j]]];
				ans+=(ll)a2[0]*a2[1]+(ll)a2[1]*a2[2]+(ll)a2[2]*a2[0];
			}
		}else ans+=sum;
	}return printf("%lld",ans),0;
}
il bool dfs(int x){
	a1[++a1t]=x;bool pd(false);
	for(int i(head[x]),i1;i;i=ar[i].next)
		if(++sum,!~a[i1=ar[i].to]){
			a[i1]=(a[x]+ar[i].w)%3;
			pd|=dfs(i1);
		}else if(a[i1]^(a[x]+ar[i].w)%3)pd|=1;
	return pd;
}
il void link(int u,int v,int w){
	ar[++art]={head[u],v,w},head[u]=art;
}
il void read(int&x){
	x^=x;ri char c;while(c=getchar(),c<'0'||c>'9');
	while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
}