#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int long long
const int MAXN = 1e5 + 7;
int low[MAXN], id[MAXN], dfstime, scc[MAXN], cnt, ans, save[MAXN], node[MAXN];
int n, m;
bool mark[MAXN];
vector <int> a[MAXN];
set <int> s[MAXN];
stack <int>st;
void dfs(int u){
low[u] = id[u] = ++dfstime;
st.push(u);
for(auto v : a[u]){
if(!mark[v]){
if(!id[v]){
dfs(v);
low[u] = min(low[v], low[u]);
}
else low[u] = min(low[u], id[v]);
}
}
if(low[u] == id[u]){
int v;
cnt++;
do{
v = st.top();
st.pop();
scc[v] = cnt;
mark[v] = 1;
node[cnt]++;
}while(u != v);
}
}
signed main(){
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
cin >> n >> m;
for(int i = 1; i <= m; i++){
int x, y;
cin >> x >> y;
if(s[x].find(y) == s[x].end()){
a[x].push_back(y);
s[x].insert(y);
}
}
for(int i = 1; i <= n; i++) if(!id[i])dfs(i);
for(int u = 1; u <= n; u++)
for(auto v : a[u])
if(scc[u] == scc[v]) save[scc[v]]++;
for(int i = 1; i <= cnt; i++){
int cur = node[i];
cur = (cur * (cur - 1));
ans += cur - save[i];
}
cout << ans;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnR5cGVkZWYgbG9uZyBsb25nIGxsOwojZGVmaW5lIGludCBsb25nIGxvbmcKY29uc3QgaW50IE1BWE4gPSAxZTUgKyA3OwppbnQgbG93W01BWE5dLCBpZFtNQVhOXSwgZGZzdGltZSwgc2NjW01BWE5dLCBjbnQsIGFucywgc2F2ZVtNQVhOXSwgbm9kZVtNQVhOXTsKaW50IG4sIG07CmJvb2wgbWFya1tNQVhOXTsKdmVjdG9yIDxpbnQ+IGFbTUFYTl07CnNldCA8aW50PiBzW01BWE5dOwpzdGFjayA8aW50PnN0Owp2b2lkIGRmcyhpbnQgdSl7CiAgICBsb3dbdV0gPSBpZFt1XSA9ICsrZGZzdGltZTsKICAgIHN0LnB1c2godSk7CiAgICBmb3IoYXV0byB2IDogYVt1XSl7CiAgICAgICAgaWYoIW1hcmtbdl0pewogICAgICAgICAgICBpZighaWRbdl0pewogICAgICAgICAgICAgICAgZGZzKHYpOwogICAgICAgICAgICAgICAgbG93W3VdID0gbWluKGxvd1t2XSwgbG93W3VdKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIGxvd1t1XSA9IG1pbihsb3dbdV0sIGlkW3ZdKTsKICAgICAgICB9CiAgICB9CiAgICBpZihsb3dbdV0gPT0gaWRbdV0pewogICAgICAgIGludCB2OwogICAgICAgIGNudCsrOwogICAgICAgIGRvewogICAgICAgICAgICB2ID0gc3QudG9wKCk7CiAgICAgICAgICAgIHN0LnBvcCgpOwogICAgICAgICAgICBzY2Nbdl0gPSBjbnQ7CiAgICAgICAgICAgIG1hcmtbdl0gPSAxOwogICAgICAgICAgICBub2RlW2NudF0rKzsKICAgICAgICB9d2hpbGUodSAhPSB2KTsKICAgIH0KfQoKc2lnbmVkIG1haW4oKXsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCk7CiAgICBjb3V0LnRpZSgwKTsKICAgIGNpbi50aWUoMCk7CiAgICBjaW4gPj4gbiA+PiBtOwogICAgZm9yKGludCBpID0gMTsgaSA8PSBtOyBpKyspewogICAgICAgIGludCB4LCB5OwogICAgICAgIGNpbiA+PiB4ID4+IHk7CiAgICAgICAgaWYoc1t4XS5maW5kKHkpID09IHNbeF0uZW5kKCkpewogICAgICAgICAgICBhW3hdLnB1c2hfYmFjayh5KTsKICAgICAgICAgICAgc1t4XS5pbnNlcnQoeSk7CiAgICAgICAgfQogICAgfQogICAgZm9yKGludCBpID0gMTsgaSA8PSBuOyBpKyspIGlmKCFpZFtpXSlkZnMoaSk7CiAgICAKICAgIGZvcihpbnQgdSA9IDE7IHUgPD0gbjsgdSsrKQogICAgICAgIGZvcihhdXRvIHYgOiBhW3VdKQogICAgICAgICAgICBpZihzY2NbdV0gPT0gc2NjW3ZdKSBzYXZlW3NjY1t2XV0rKzsKICAgICAgICAKICAgIGZvcihpbnQgaSA9IDE7IGkgPD0gY250OyBpKyspewogICAgICAgIGludCBjdXIgPSBub2RlW2ldOwogICAgICAgIGN1ciA9IChjdXIgKiAoY3VyIC0gMSkpOwogICAgICAgIGFucyArPSBjdXIgLSBzYXZlW2ldOwogICAgfQogICAgY291dCA8PCBhbnM7Cn0=