#include <bits/stdc++.h>
using namespace std;

int n,m,c=0;
vector<bool>vis;
 vector<vector<int>>adj;
 void dfs(int node)
 {
     vis[node]=true;
     for(auto child: adj[node])
{
      if (!vis[child])
     {
         c++;
          dfs(child);
     }
}
}
int main() {
    int i,j;
   cin>>n>>m;
   adj.resize(n);
   vis.resize(n);
   for(i=0;i<m;i++)
   {
       int a,b;
       cin>>a>>b;
       a--;b--;
       adj[a].push_back(b);
   }
   for(i=0;i<n;i++)
   {
       for(j=0;j<n;j++)
       vis[j]=false;
       dfs(i);
   }
   cout<<c+n;
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, s, n) for(int i = (int)(s); i < (int)(n); ++i)

ll INF = 1ll << 60;

vi G[2010];
bool ck[2010];

void init(){
    rep(i, 0, 2010){
        ck[i] = false;
    }
}

void dfs(int v){
    ck[v] = true;
    for(auto nv : G[v]){
        if(ck[nv]){
            continue;
        }
        dfs(nv);
    }
}

int main(){
    int n, m;
    cin >> n >> m;
    rep(i, 0, m){
        int a, b;
        cin >> a >> b;
        a--;b--;
        G[a].push_back(b);
    }
    ll ans = 0;

    rep(i, 0, n){
        init();
        ll cnt = 0;
        dfs(i);
        rep(i, 0, 2010){
            if(ck[i]){
                cnt++;
            }
        }
        ans += cnt;
    }

    cout << ans << endl;
    
    return 0;
    
}