#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using Graph = vector<vector<int>>;
using ll = long long;
using P = pair<int, int>;

vector<bool> seen;
void dfs(const Graph &G, int v) {
    seen[v] = true;


    for (auto next_v : G[v]) { 
        if (seen[next_v]) continue;
        dfs(G, next_v); 
    }
}


int main() {
    int N, M; cin >> N >> M;

    Graph G(N);
    for (int i = 0; i < M; ++i) {
        int a, b;
        cin >> a >> b;
        G[a-1].push_back(b-1);
    }
    int ans = 0;
    rep(i,N){
            seen.assign(N, false);
            dfs(G, i);
        rep(j,N){
            if(seen.at(j)){
                ans++;
            }
        }
    }
    cout << ans << endl;

}
#include<bits/stdc++.h>
#define forn(i,a,b)for(int i=(a),_b=(b);i<=_b;i++)
#define fore(i,b,a)for(int i=(b),_a=(a);i>=_a;i--)
#define rep(i,n)for(int i=0,_n=n;i<n;i++)
#define ll long long
#define pii pair<int,int>
#define m_p make_pair
#define pb push_back
#define fi first
#define se second
#define foreach(i,c) for(__typeof(c.begin())i=(c.begin());i!=(c).end();i++)
using namespace std;
const int N=2010;
int n,m;
vector<int>g[N];
int vis[N]; 
int ans=0;
void dfs(int u){
	vis[u]=1;
	ans++;
	rep(i,g[u].size()){
		int v=g[u][i];
		if(vis[v])continue;
		dfs(v);
	}
} 
int main(){
	ios::sync_with_stdio(0);
	cin>>n>>m;
	rep(i,m){
		int u,v;
		cin>>u>>v;
		u--;v--;
		g[u].pb(v);
	}
	rep(i,n){
		memset(vis,0,sizeof(vis));
		dfs(i);
	}
	cout<<ans<<"\n";
	return 0;
}
