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

int n,m,ans;

int now[2008],to[20000],pre[20000],tot;

void add(int x,int y) {
	to[++tot]=y;
	pre[tot]=now[x];
	now[x]=tot;
}

bool vis[20000];
void dfs(int u) {
	vis[u]=1;
	ans++;
	for(int i=now[u]; i; i=pre[i]) {
		int v=to[i];
		if(!vis[v]) dfs(v);
	}
}


int main() {
	cin>>n>>m;
	for(int i=1,x,y; i<=m; i++) {
		cin>>x>>y;
		add(x,y);
	}
	for(int i=1; i<=n; i++) {
		memset(vis,0,sizeof vis);
		dfs(i);
	}
	printf("%d\n",ans);
	return 0;
}

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

vector<vector<int>> g;
vector<int> visited;

int dfs(int v) {
  int res = 1;
  visited[v] = 1;
  for (auto nv : g[v]) {
    if (visited[nv]) continue;
    res += dfs(nv);
  }
  return res;
}

int main() {
  int n, m;
  cin >> n >> m;
  g.resize(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g[a].push_back(b);
  }
  int ans = 0;
  rep(i, n) {
    visited.assign(n, 0);
    ans += dfs(i);
  }
  cout << ans << endl;
  return 0;
}
