#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iomanip>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cstdint>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
#define all(x) (x).begin(),(x).end()

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}
 
template<class T> inline bool chmax(T &a, T b){
    if(a<b){
        a = b;
        return true;
    }
    return false;
}

template<class T> inline bool chmin(T &a, T b){
    if(a>b){
        a = b;
        return true;
    }
    return false;
}
class UnionFind {
private:
    int sz;
    vector<int> par, size_;
public:
    UnionFind(){}
    UnionFind(int node_size) : sz(node_size), par(sz), size_(sz, 1){
        iota(par.begin(), par.end(), 0);
    }
    int find(int x){
        if(par[x] == x) return x;
        else return par[x] = find(par[x]);
    }
    void unite(int x,int y){
        x = find(x), y = find(y);
        if(x == y) return;
        if(size_[x] < size_[y]) swap(x,y);
        par[y] = x;
        size_[x] += size_[y];
    }
    int size(int x){
        x = find(x);
        return size_[x];
    }
    bool same(int x,int y){
        return find(x) == find(y);
    }
};


vector<vector<int> > g;
vector<vector<int> > rev;
vector<int> c;
vector<bool> ng,used;
UnionFind uf;

int nxt(int d){
    d++;
    if(d==4)return 1;
    return d;
}
int pre(int d){
    d--;
    if(d==0)return 3;
    return d;
}

void dfs(int id){
    if(c[id]==0){
        c[id] = 1;
    }
    used[id] = true;
    for(auto x : g[id]){
        if(used[x]){
            if(c[x]!=nxt(c[id])){
                ng[uf.find(id)] = 1;
            }
        }else{
            c[x] = nxt(c[id]);
            dfs(x);
        }
    }
    for(auto x : rev[id]){
        if(used[x]){
            if(c[x]!=pre(c[id])){
                ng[uf.find(id)] = 1;
            }
        }else{
            c[x] = pre(c[id]);
            dfs(x);
        }
    }
}

int main(){
    int n,m;
    cin >> n >> m;
    g.resize(n);
    ng.resize(n);
    rev.resize(n);
    used.resize(n);
    c.resize(n);
    uf=UnionFind(n);
    vector<pair<int,int> > ss;
    rep(i,m){
        int a,b;
        cin >> a >> b;
        a--;b--;
        uf.unite(a,b);
        g[a].push_back(b);
        rev[b].push_back(a);
        ss.push_back(MP(a,b));
    }
    for(int i=0;i<n;i++){
        if(used[i])continue;
        dfs(i);
    }
    vector<vector<ll> > a(n,vector<ll>(3));
    vector<ll>b(n);
    for(int i=0;i<n;i++){
        if(ng[uf.find(i)]){
            b[uf.find(i)]++;
        }else{
            a[uf.find(i)][c[i]-1]++;
        }
    }
    vector<ll> P(n);
    for(auto x:ss){
        P[uf.find(x.first)]++;
    }
    ll res = 0;
    for(int i=0;i<n;i++){
        // res += a[i][0]*a[i][1] + a[i][1]*a[i][2] + a[i][2]*a[i][0];
        if(a[i][2]!=0&&a[i][1]!=0)res += a[i][0]*a[i][1] + a[i][1]*a[i][2] + a[i][2]*a[i][0];
        else if(!ng[i]) res += P[i];
        else res += b[i]*b[i];
    }
    cout << res << endl;
    return 0;
}

Measured memory_bytes: 26484