#include<bits/stdc++.h>

#define x first
#define y second
#define PII pair<int,int>
#define LL long long 

using namespace std;

const int N=2010;

int n,m;
int h[N],e[N],ne[N],idx;
int ans;
bool st[N];

void add(int a,int b){
   e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}

void dfs(int u) {
    st[u]=true;
	for (int i=h[u];~i;i=ne[i]){
        if (!st[e[i]]) {
        	ans++;
            dfs(e[i]);
		}
    }
}

int main(){
	
	memset(h,-1,sizeof h);
	
	scanf("%d%d",&n,&m);
	
	if(m==0){
		printf("%d\n",n);
		return 0;
	}
	ans=n;
	
	while(m--){
		int a,b;
		scanf("%d%d",&a,&b);
		add(a,b);
	}
	
	for(int i=1;i<=n;i++){
		memset(st,0,sizeof st);
		dfs(i);
	}
	
	printf("%d\n",ans);
	
	return 0;
}
#include<bits/stdc++.h>
// no of bits to store no 2e5 is 19
// 2e5 ka 5 times square root lene se 2 or 3 ke pass pahunch jate hai
using namespace std;

#define int               long long
#define mod               1000000007
#define MOD               1000000007
#define MAX               1000000000
#define inf               2e18
#define ff                first
#define ss                second
#define mp                make_pair
#define pb                push_back
#define pii               pair<int,int>
#define mii               map<int, int>
#define all(x)            (x).begin(), (x).end()
#define fill(a,b)         memset(a, b, sizeof(a))
#define sp(x,y)           fixed<<setprecision(y)<<x
#define setbits(x)        __builtin_popcount(x)
#define fast_in_out       ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define rep(i,a,b)        for(int i=a;i<b;i++)
#define ll                long long
#define endl              '\n'


//   powermod(a,b,m) ==  powermod(a,b%(m-1),m)  when m is prime fermat's theorem a^(p-1) %p=1 p is prime
int powermod(int a,int b,int c) //  gives (a^b)%c
{
    int ans = 1,tempans=a%c;
    while(b>0)
    {
        if((b&1)==1)
        {
            ans=(ans*tempans)%c;
        }
             tempans=(tempans*tempans)%c;
             b=b>>1;
    }
    return ans;
}

int modinverse(int a,int m)   // gives a inverse wrt m and m is prime
{
    return powermod(a,m-2,m);
}

ll add(ll x, ll y) {
   x += y;
   if (x >= mod) return x - mod;
   return x;
}
ll sub(ll x, ll y) {
   x -= y;
   if (x < 0) return x + mod;
   return x;
}
ll mult(ll x, ll y) {
   return (x * y) % mod;
}
ll inv(ll x)
{
    return powermod(x, MOD - 2,mod);
}

ll divide(ll x, ll y)
{
    return mult(x, inv(y));
}

// my template                                              m
// prime factorization                                      pf
// ncr and factorials                                       ncr
// matrix exponentiation of fibonacci series in log(n)      matexpofib
// cycles in a graph                                        cycle
// prime factorization in log(n)                            spf
// disjoint set union                                       dsu

vector<int> topo;int n,m;
vector<int> edges[2005];
int vis[2005];
int y=0;
void dfs(int i)
{
   vis[i]=1;
   y++;
   for(auto ch:edges[i])
   {
      if(!vis[ch])
      {
         dfs(ch);
      }
   }

}

void solve()
{
   cin>>n>>m;

   while(m--)
   {
      int x,y;
      cin>>x>>y;
      edges[x-1].pb(y-1);
   }

  int ans =0;
  for(int i=0;i<n;i++)
  {
     for(int j=0;j<n;j++)
      vis[j]=0;
      y=0;
     dfs(i);
     //cout<<y<<endl;
     ans+=(y);

  }
  cout<<ans;
}


int32_t main()
{
  fast_in_out;

  int t=1;//cin>>t;
  int count=0;


   while(t--)
   {
       count++;
       solve();
   }
}
