#include <bits/stdc++.h>
using namespace std;
 
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long


const int N = 2e3+5;
vector<int>g[N];
bool vis[N];
int cnt = 0;

void dfs(int src)
{
    vis[src] = true;
    cnt++;
    for(auto  i :g[src]) if(!vis[i]) dfs(i);
}



int32_t main()
{
    IOS;
    int n,m;
    cin>>n>>m;
    for(int i = 0;i<m;i++)
    {
        int u,v;
        cin>>u>>v;
        g[u].push_back(v);
    }
    int ans = 0;
    for(int i = 1;i<=n;i++)
    {
        dfs(i);
        //cout<<i<<" "<<cnt<<endl;
        ans += cnt;
        cnt = 0;
        memset(vis,0,sizeof(vis));
    }
    cout<<ans<<endl;
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD=1000000007;
#define INF 1LL<<30
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()

vector<vector<int>> G;
bool temp[2010];
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);
    rep(i,M){
        int a,b;
        cin>>a>>b;
        G[a-1].push_back(b-1);
    }

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

    cout<<ans<<endl;
}

