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<iostream>
#include<algorithm>
using namespace std;
#define ll long long

struct node { int t,v,next,from; }a[200010];

int head[100010],num[100010],n,m,h1,h2,h3,cnt,cnt1,tot;
bool bo[100010],bb[100010],flag;
ll ans;

inline int rd()
{
	int x=0;char ch=getchar();
	for (;ch<'0'||ch>'9';ch=getchar());
	for (;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
	return x;
}

inline void add(int x,int y,int z,int hh) { a[++tot].t=y;a[tot].v=z;a[tot].from=hh;a[tot].next=head[x];head[x]=tot; }

inline void dfs(int x)
{
	bo[x]=true;cnt++;
	if (!num[x]) h1++;
	else if (num[x]==1) h2++;
	else h3++;
	for (int i=head[x];i;i=a[i].next)
	{
		int t=a[i].t;
		if (bb[a[i].from]) continue;
		bb[a[i].from]=true;cnt1++;
		if (!bo[t]) num[t]=(num[x]+a[i].v+3)%3,dfs(t);
		else if (num[t]!=(num[x]+a[i].v+3)%3) flag=true;
	}
}

int main()
{
	n=rd();m=rd();tot=0;
	for (int i=1;i<=m;i++)
	{
		int x=rd(),y=rd();
		add(x,y,1,i);add(y,x,-1,i);
	}
	ans=0LL;
	for (int i=1;i<=n;i++)
	{
		flag=false;
		h1=h2=h3=cnt=cnt1=0;
		dfs(i);
		if (flag) { ans+=(ll)cnt*cnt;continue; }
		if (!h1||!h2||!h3) { ans+=(ll)cnt1;continue; }
		ans+=(ll)h1*h2+(ll)h2*h3+(ll)h3*h1;
	}
	printf("%lld\n",ans);
	return 0;
}