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;

int N, M, A[1 << 18], B[1 << 18], dist[1 << 18];
vector<pair<int, int>> X[1 << 18];
vector<int> G;
bool flag = false;

void dfs(int pos, int dep) {
	if (dist[pos] >= 0) {
		if (dist[pos] != dep) flag = true;
		return;
	}
	G.push_back(pos);
	dist[pos] = dep;
	for (int i = 0; i < X[pos].size(); i++) {
		dfs(X[pos][i].first, (dep + X[pos][i].second) % 3);
	}
}

int main() {
	cin >> N >> M;
	for (int i = 0; i < M; i++) {
		cin >> A[i] >> B[i];
		X[A[i]].push_back(make_pair(B[i], 1));
		X[B[i]].push_back(make_pair(A[i], 2));
	}
	
	for (int i = 1; i <= N; i++) dist[i] = -1;
	
	long long ans = 0;
	for (int i = 1; i <= N; i++) {
		if (dist[i] >= 0) continue;
		flag = false; G.clear();
		dfs(i, 0);
		
		if (flag == true) ans += 1LL * (long long)(G.size()) * (long long)(G.size());
		else {
			long long c[3] = {0, 0, 0};
			for (int j : G) c[dist[j]] += 1;
			if (c[0] * c[1] * c[2] >= 1) ans += c[0] * c[1] + c[1] * c[2] + c[2] * c[0];
			else {
			    map<pair<int, int>, int> Map;
			    for (int pos : G) {
			        for (pair<int, int> to : X[pos]) {
			            if (to.second == 1 && Map[make_pair(pos, to.first)] == 0) {
			                ans++;
			                Map[make_pair(pos, to.first)] = 1;
			            }
			        }
			    }
			}
		}
	}
	cout << ans << endl;
	return 0;
}