#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;
}
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, x, y) for (int i = x; i < y; i++)

vector<vector<bool>> ok;

void dfs(map<int, set<int>> &path, int i, int p)
{
    if (ok[p][i])
        return;
    ok[p][i] = true;
    for (auto v : path[i])
        dfs(path, v, p);
}

int main()
{
    int N, M;
    cin >> N >> M;
    map<int, set<int>> path;
    ok.resize(N);
    rep(i, 0, N) ok[i].resize(N);
    rep(i, 0, M)
    {
        int A, B;
        cin >> A >> B;
        path[A - 1].insert(B - 1);
    }
    ll ans = 0;
    rep(i, 0, N)
    {
        dfs(path, i, i);
        ans += count_if(ok[i].begin(), ok[i].end(), [](bool f)
                        { return f; });
    }
    cout << ans << endl;
}