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
//besmellah
#include <bits/stdc++.h>
using namespace std;

#define int long long
const int maxn = 1e5 + 87;
int sz[3], mark[maxn], M;
vector <int> adj[maxn], radj[maxn];
bool T;

bool dfs(int v) {
	sz[mark[v]]++;
	bool f = 1;
	M += adj[v].size();
	T = T | ((bool)adj[v].size() & (bool)(radj[v].size()));
	for (auto u: adj[v]) {
		if (mark[u] == -1) {
			mark[u] = (mark[v] + 1) % 3;
			f = dfs(u) & f;
		}
		else if (mark[u] != (mark[v] + 1) % 3) f = 0;
	}
	for (auto u: radj[v]) {
		if (mark[u] == -1) {
			mark[u] = (mark[v] + 2) % 3;
			f = dfs(u) & f;
		}
		else if (mark[u] != (mark[v] + 2) % 3) f = 0;
	}
	return f;
}

int32_t main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		int v, u;
		cin >> v >> u;
		v--;
		u--;
		adj[v].push_back(u);
		radj[u].push_back(v);
	}
	int ans = 0;
	fill(mark, mark + n, -1);
	for (int i = 0; i < n; i++) {
		if (mark[i] == -1) {
			T = 0, M = 0;
			mark[i] = 0;
			sz[0] = sz[1] = sz[2] = 0;
			if (dfs(i)) {
				if (T)
					ans += sz[0] * sz[1] + sz[1] * sz[2] + sz[2] * sz[0];
				else ans += M;
//				cout << "SAL " << endl;
			}
			else {
				ans += (sz[0] + sz[1] + sz[2]) * (sz[0] + sz[1] + sz[2]);
//				cout << "AJAB" << ' ' << sz[0] << ' ' << sz[1] << ' ' << sz[2] << ' ' << ans << endl;
			}
		}
	}
	cout << ans;
}
