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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<P> vp;
typedef vector<vp> vvp;
const int inf=1<<30;

ll n,m,V,E,d[3];
vvp g;
vi a;
vp b;

bool dfs(int v){
	V++;
	bool A=0,B=0,flag=0;
	for(auto p:g[v]){
		int u=p.first,t=p.second;
		if(a[u]==inf){
			a[u]=(a[v]+t+1)%3;
			flag|=dfs(u);
		}
		else{
			if(t) b.push_back({u,v});
			else b.push_back({v,u});
		}
		if(t) A=1,E++;
		else B=1;
	}
	d[a[v]]++;
	if(A&&B) flag=1;
	return flag;
}

ll f(int x){
	a[x]=V=E=0;
	b.clear();
	fill(d,d+3,0);
	if(!dfs(x)) return E;
	bool B=0;
	for(auto p:b) if((a[p.first]-a[p.second]+4)%3>0) B=1;
	if(B) return V*V;
	return d[0]*d[1]+d[1]*d[2]+d[2]*d[0];
}

int main(){
	cin>>n>>m;
	a=vi(n,inf);
	g=vvp(n);
	for(int i=0;i<m;i++){
		int u,v;
		cin>>u>>v;
		u--;v--;
		g[u].push_back({v,0});
		g[v].push_back({u,1});
	}
	ll res=0;
	for(int i=0;i<n;i++) if(a[i]==inf) res+=f(i);
	cout<<res<<endl;
}