#include<bits/stdc++.h>
using namespace std;
void dfs(vector<vector<int > > &g,int s,int &n,int &m,int &ans,vector<int > &vis)
{
  if(vis[s])
    return;
 
  vis[s]=1;
  for(unsigned int i=0;i<g[s].size();i++)
  {
    if(!vis[g[s][i]])
    { ans++;
    dfs(g,g[s][i],n,m,ans,vis);
    }
  }
  return ;
}
int main()
{
  int n;
  int m;
  cin>>n>>m;
  vector<vector<int> > g(n);
  
  for(int i=0;i<m;i++)
  {
    int x,y;
    cin>>x>>y;
    x=x-1;
    y=y-1;
    g[x].push_back(y);
  }
  int ans= n;
  
  for(int i=0;i<n;i++)
  { vector<int> vis;
   for(int j=0;j<n;j++)
     vis.push_back(0);
   int a=0;
    
    dfs(g,i,n,m,a,vis) ;
   ans+=a;
   // ans+=(long long int) (a  );
  // cout<<"i"<<i<<" "<<a<<endl;
  }
  cout<<ans<<endl;
  return 0;
}
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using Graph = vector<vector<int>>;

ll ans = 0;
vector<bool> seen;

void dfs(Graph &g,int v){
   
   seen.at(v) = true;
   ans++;
   for(auto nv:g.at(v)){
       if(!seen.at(nv)){
       dfs(g,nv);
       }
   }
   return;
}
int main(){
   int n,m;
   cin >> n >> m;
   Graph g(n);
   rep(i,m){
       int a,b;
       cin >> a >> b;
       a--; b--;
       g.at(a).push_back(b);
   }

   rep(i,n){
       seen.assign(n, false);
       dfs(g,i);
   }
   cout << ans << endl;
}