#include <bits/stdc++.h>
struct Edge {
  int y, frt;
} g[101000 * 2];
int n, m, pos[101000], gsz, cnt_e, col[101000];
long long cnt[3], ans;
bool invalid;
void AE(int x, int y) { g[++gsz] = (Edge){y, pos[x]}, pos[x] = gsz; }
void dfs(int x) {
  ++cnt[col[x]];
  for (int i = pos[x]; i; i = g[i].frt) {
    int c = (col[x] + (i % 2 == 1 ? 1 : 2)) % 3;
    if (col[g[i].y] == -1)
      col[g[i].y] = c, dfs(g[i].y);
    else if (col[g[i].y] != c)
      invalid = true;
    ++cnt_e;
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    AE(x, y), AE(y, x);
  }
  for (int i = 1; i <= n; ++i) col[i] = -1;
  for (int i = 1; i <= n; ++i)
    if (col[i] == -1) {
      cnt[0] = cnt[1] = cnt[2] = cnt_e = 0;
      col[i] = 0;
      invalid = false;
      dfs(i);
      if (invalid)
        ans += (cnt[0] + cnt[1] + cnt[2]) * (cnt[0] + cnt[1] + cnt[2]);
      else if (cnt[1] != 0 && cnt[2] != 0)
        ans += cnt[0] * cnt[1] + cnt[1] * cnt[2] + cnt[2] * cnt[0];
      else
        ans += cnt_e / 2;
    }
  printf("%lld\n", ans);
  return 0;
}
