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>
#define neko 100010
#define f(i,a,b) for(register int i=(a);i<=(b);i=-(~i))
#define travel(i,u,v) for(register int i=head[u],v=e[i].v;i;i=e[i].nex,v=e[i].v)
typedef long long ll;
int n,m,t,flag;
typedef int arr[neko];
arr head,col,book;
struct edge
{
	int v,tp,nex;
}e[neko<<1];
ll ans,tot,cnt[5];
void dfs(int u)
{
	book[u]=1,++cnt[0],++cnt[col[u]];
	travel(i,u,v)
	{
		if(e[i].tp==1)++tot;
		if(!book[v])
		{
			col[v]=(col[u]+e[i].tp+2)%3+1;//反正这样就是对的我不管啦
			dfs(v);
		}
		else if(col[v]^((col[u]+e[i].tp+2)%3+1))flag=1;
	}
}
void add(int x,int y)
{
	e[++t].v=y,e[t].tp=1,e[t].nex=head[x],head[x]=t;
	e[++t].v=x,e[t].tp=-1,e[t].nex=head[y],head[y]=t;
}
int main()
{
	int x,y;
	scanf("%d%d",&n,&m);
	f(i,1,m)scanf("%d%d",&x,&y),add(x,y);
	f(i,1,n)col[i]=1;
	f(i,1,n)
	{
		if(book[i])continue;
		cnt[0]=tot=cnt[1]=cnt[2]=cnt[3]=0;
		dfs(i);
		if(flag)ans+=cnt[0]*cnt[0],flag=0;
		else if(!(cnt[1]&&cnt[2]&&cnt[3]))ans+=tot;
		else ans+=cnt[1]*cnt[2]+cnt[2]*cnt[3]+cnt[3]*cnt[1];
	}return printf("%lld\n",ans),0;
}