#include<bits/stdc++.h>
using namespace std;
const int MAX_N = 2000;

vector<vector<int>>G;
bool temp[MAX_N];
void dfs(int v) {
    if (temp[v])return;
    temp[v] = true;
    for (auto vv : G[v])dfs(vv);
}

int main() {
    int N, M;
    cin >> N >> M;
    G.resize(N);
    for (int i = 0; i < M; i++) {
        int a, b;
        cin >> a >> b;
        G[a - 1].push_back(b - 1);
    }

    int ans = 0;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++)temp[j] = false;
        dfs(i);
        for (int j = 0; j < N; j++)if (temp[j])ans++;
    }

    cout << ans;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+5;
int mod=1e9+7;
int gcd(int x,int y)
{
    if(y==0)
    {
        return x;
    }
    return gcd(y,x%y);
}
int power(int a,int n)
{
    int res=1;
    while(n)
    {
        if(n%2)
        {
            res=(res*a)%mod;
        }
        n=n/2;
        a=(a*a)%mod;
    }
    return res;
}
vector<vector<int>> adj;
vector<bool> vis;
void dfs(int v)
{
    vis[v]=true;
    int i,n=adj[v].size();
    for(i=0;i<n;i++)
    {
        if(!vis[adj[v][i]])
        {
            dfs(adj[v][i]);
        }
    }
}
void solve()
{
    int i,n,m,x,y,cnt=0;
    cin>>n>>m;
    adj.resize(n+1);
    for(i=0;i<m;i++)
    {
        cin>>x>>y;
        adj[x].push_back(y);
    }
    for(i=1;i<=n;i++)
    {
        vis.assign(n,false);
        dfs(i);
        for(int j=1;j<=n;j++)
        {
            cnt=cnt+vis[j];
        }
    }
    cout<<cnt<<"\n";
}
int32_t main()
{
    int t=1;
    while(t--)
    {
        solve();
    }
    return 0;
}
