#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
typedef long long ll;
#define rep(i, a, b) for (int i = a; i <= b; ++ i)
const int maxn = 100010;
using namespace std;
bool vis[maxn]; int col[maxn]; ll h[3], ans;
int n, m, e, ter[maxn << 1], nxt[maxn << 1], lnk[maxn], opt[maxn];
void add(int x, int y) {
	ter[++ e] = y, nxt[e] = lnk[x], lnk[x] = e;
	ter[++ e] = x, nxt[e] = lnk[y], lnk[y] = e;
}
void bfs(int st) {
	int he = 0, ta = 1, ed = 0; opt[1] = st, vis[st] = false;
	col[st] = 0; bool flg = true;
	while (he != ta) {
		++ he; int u = opt[he];
		for (int i = lnk[u]; i; i = nxt[i]) {
			if (i & 1) ++ ed;
			if (vis[ter[i]]) {
				vis[ter[i]] = false;
				if (i & 1) col[ter[i]] = (col[u] + 1) % 3;
					else col[ter[i]] = (col[u] + 2) % 3;
				opt[++ ta] = ter[i];
			} else {
				if (i & 1) flg &= (col[ter[i]] == (col[u] + 1) % 3);
					else flg &= (col[ter[i]] == (col[u] + 2) % 3);
			}
		}
	}
	if (flg) {
		h[0] = h[1] = h[2] = 0;
		rep(i, 1, ta) ++ h[col[opt[i]]];
		if (!h[0] || !h[1] || !h[2]) { ans += ed; return ; }
		ans += h[0] * h[1] + h[1] * h[2] + h[2] * h[0];
	} else ans += (ll) ta * ta;
}
int main() {
	scanf("%d%d", &n, &m); int x, y;
	rep(i, 1, m) scanf("%d%d", &x, &y), add(x, y);
	memset(vis, true, sizeof(vis));
	ans = 0;
	rep(i, 1, n) if (vis[i]) bfs(i);
	printf("%lld\n", ans);
	return 0;
}