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 <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int L[101010];
vector<pair<int,int>> E[101010];
int N,M;
ll NL[3],NG,NE;

void dfs(int cur,int pre) {
	NL[L[cur]]++;
	FORR(r,E[cur]) {
		if(r.second==1) NE++;
		if(L[r.first]==-1) {
			L[r.first]=(L[cur]+r.second)%3;
			dfs(r.first,cur);
		}
		else {
			if(L[r.first]!=(L[cur]+r.second)%3) NG++;
		}
	}
	
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>M;
	FOR(i,M) {
		cin>>x>>y;
		E[x-1].push_back({y-1,1});
		E[y-1].push_back({x-1,2});
	}
	
	MINUS(L);
	ll ret=0;
	FOR(i,N) if(L[i]==-1) {
		NL[0]=NL[1]=NL[2]=0;
		NG=NE=0;
		L[i]=0;
		dfs(i,-1);
		if(NG) ret += (NL[0]+NL[1]+NL[2])*(NL[0]+NL[1]+NL[2]);
		else if(NL[1]==0 || NL[2]==0) ret += NE;
		else ret += NL[0]*NL[1]+NL[1]*NL[2]+NL[2]*NL[0];
	}
	cout<<ret<<endl;
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n';
	FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	solve(); return 0;
}