#include <iostream>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <functional>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
using namespace std;
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i < (n); ++i)
#define srep(i, s, n) for (int i = s; i < (n); ++i)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
using T3 = tuple<int, int, int>;
template<typename T>void print(vector<T> a) { for (T t : a) { cout << t << " "; } cout << endl; }
const ll LINF = 1LL << 60;
const int INF = 1001001001;
const int mod = 1e9 + 7;

vector<vector<int> > g;
vector<bool> visited;

ll f(int i) {
    ll res = 0;

    // for (int next : g[i])
    // res += g[i].size();
    for (int next : g[i]) {
        if (!visited[next]) {
            visited[next] = true;
            res++;
            res += f(next); 
        }
    }
    return res;
}

int main() {
    int n, m;
    cin >> n >> m;
    g = vector<vector<int> >(n);
    rep(i, n) { g[i].pb(i); }
    rep(i, m) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        g[a].pb(b);
    }

    // rep(i, n) { print(g[i]); }

    ll ans = 0;
    rep(i, n) {
        visited = vector<bool>(n, false);
        // cout << ans << endl;
        ans += f(i);
    }
    cout << endl;
    cout << ans << endl;
    return 0;
}
#include <bits/stdc++.h>
#define ll long long
#define rgi register int
using namespace std;
const int M=2e3+7,modd=998244353;
int n,m;
bitset<M>a[M];
inline int read(){
	int w=0,r=1;char c=getchar();
	while(!(isdigit(c)||c=='-'))c=getchar();
	if(c=='-'){r=-1;c=getchar();}
	while(isdigit(c)){w=(w<<3)+(w<<1)+c-'0';c=getchar();}
	return w*r;
}
int main(){
	int t=1;
	while(t--){
		n=read(),m=read();
		for(int i=1;i<=m;i++){
			int fr=read(),tr=read();
			a[fr][tr]=1;
		}
		for(int i=1;i<=n;i++)a[i][i]=1;
		for(int k=1;k<=n;k++){
			for(int i=1;i<=n;i++){
				if(a[i][k])a[i]|=a[k];
			}
		}
		int ans=0;
		for(int i=1;i<=n;i++)ans+=a[i].count();
		printf("%d\n",ans);
	} 
	return 0;
}
/*
3 3
1 2
2 3
3 2
*/