#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template<typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
template<typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); }
template<typename T> bool mmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template<typename T> bool mmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }
template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
#define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl;

using tpl = tuple<lint, lint, lint>;
struct SizeAwareUnionFind
{
    std::vector<int> par;
    std::vector<tpl> cou;
    lint v(tpl t) {
        lint x, y, z;
        tie(x, y, z) = t;
        return x + y + z;
    }
    void add(tpl &x, tpl y) {
        lint a, b, c, d, e, f;
        tie(a, b, c) = x;
        tie(d, e, f) = y;
        x = tpl(a + d, b + e, c + f);
    }
    SizeAwareUnionFind(int N = 0) : par(N), cou(N, tpl(0, 0, 0)) {
        iota(par.begin(), par.end(), 0);
    }
    int find(int x) { return (par[x] == x) ? x : (par[x] = find(par[x])); }
    bool unite(int x, int y) {
        x = find(x), y = find(y);
        if (x == y) return false;
        if (v(cou[x]) < v(cou[y])) std::swap(x, y); 
        par[y] = x, add(cou[x], cou[y]);
        return true;
    }
    tpl count(int x) { return cou[find(x)]; }
    bool same(int x, int y) { return find(x) == find(y); }
};

int main()
{
    int N, M;
    cin >> N >> M;
    SizeAwareUnionFind uf(N * 3);
    vector<pint> edges;
    REP(i, 3 * N) uf.cou[i] = tpl(i < N, (i >= N and i < 2 * N), i >= 2 * N);
    while (M--) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        edges.emplace_back(a, b);
        REP(d, 3) {
            int l = d * N + a, r = ((d + 1) * N + b) % (3 * N);
            uf.unite(l, r);
        }
    }
    lint ret = 0;
    vector<int> r_view(N * 3);
    vector<int> ecnt(N * 3);
    for (auto p : edges) REP(d, 3) ecnt[uf.find(p.first + d * N)]++;
    REP(i, N) {
        if (r_view[uf.find(i)]) continue;
        tpl t = uf.count(i);
        if (uf.same(i, i + N)) {
            lint sz = uf.v(t) / 3;
            ret += sz * sz;
        }
        else {
            lint nx, ny, nz;
            tie(nx, ny, nz) = t;
            if (nx and ny and nz) ret += nx * ny + ny * nz + nz * nx;
            else {
                ret += ecnt[uf.find(i)];
            }
        }
        REP(d, 3) r_view[uf.find(d * N + i)] = 1;
    }
    cout << ret << endl;
}


Measured memory_bytes: 11764