#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)

typedef long long ll;
const ll INFll = 1LL << 60;

int main(){
    //input, initialize
    int N, M; cin >> N >> M;
    vector<vector<int>> A(N);
    rep(i, M){
        int a, b;
        cin >> a >> b; a--; b--;
        A[a].push_back(b);
    }
    //solve
    ll ans = 0;
    rep(s, N){
        vector<int> dist(N, -1);
        queue<int> que;
        dist[s] = 0; que.push(s);
        while(!que.empty()){
            int v = que.front(); que.pop();
            for(auto nv: A[v]){
                if(dist[nv] == -1){
                    dist[nv] = dist[v] + 1;
                    que.push(nv);
                }
            }
        }

        rep(t, N){
            if(dist[t] != -1) ans++;
        }
    }
    //output
    cout << ans << '\n';
}
#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define ll long long
#define vi std::vector<ll>
#define si set<ll>
#define pb push_back

vi v[2020];
bool visited[2020];
ll ans;

void dfs(ll s){
    if(visited[s])return;
    visited[s] = true;
    ans++;
    for (auto u: v[s]){
        dfs(u);
    }
    return;
}

ll solve(){
    for (int i = 0; i < 2020; ++i){
        visited[i] = false;
        v[i].clear();
    }

    ll n, m;
    cin >> n >> m;

    for (int i = 0; i < m; ++i){
        ll a, b;
        cin >> a >> b;
        v[a].pb(b);
    }
    ll totans = 0;

    for (ll i = 1; i <= n; ++i){
        memset(visited, false, 2020);
        ans = 0;
        dfs(i);
        totans += ans;
    }
    cout << totans;
    return 0;
}

int main(){
    fastio;
    ll t= 1;
   // cin >> t;
    for (int i = 0; i < t; ++i){
       // cout << "Case #" << i+1 << ": ";
        solve();
        cout << "\n";
    }
    return 0;
}