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>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int MAXN=100005;

struct Edge
{
	int v,dir,id;
	Edge(int _v,int _dir,int _id)
	{v=_v;dir=_dir;id=_id;}
};

int n,m;
vector<Edge> adj[MAXN];
bool vis[MAXN],visE[MAXN];
int col[MAXN],cnt[3];
int vcnt,ecnt;
bool conflict;

void dfs(int u)
{
	vis[u]=true;
	vcnt++;
	cnt[col[u]]++;
	for(auto e:adj[u])
		if(!visE[e.id])
		{
			visE[e.id]=true;
			ecnt++;
			if(!vis[e.v])
			{
				col[e.v]=(col[u]+e.dir+3)%3;
				dfs(e.v);
			}
			else if(col[e.v]!=(col[u]+e.dir+3)%3)
				conflict=true;
		}
}

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
	{
		int a,b;
		scanf("%d%d",&a,&b);
		adj[a].push_back(Edge(b,1,i));
		adj[b].push_back(Edge(a,-1,i));
	}
	long long ans=0;
	for(int i=1;i<=n;i++)
		if(!vis[i])
		{
			cnt[0]=cnt[1]=cnt[2]=0;
			vcnt=ecnt=0;
			conflict=false;
			dfs(i);
			if(conflict)
				ans+=1LL*vcnt*vcnt;
			else if(cnt[0]==0||cnt[1]==0||cnt[2]==0)
				ans+=ecnt;
			else
				ans+=1LL*cnt[0]*cnt[1]+1LL*cnt[1]*cnt[2]+1LL*cnt[0]*cnt[2];
		}
	printf("%lld\n",ans);
	
	return 0;
}
