#include <bits/stdc++.h>
using namespace std;
vector<int> G[100005], g[100005], st;
int vis[100005];
int mk[100005];
int flag;
void dfs(int v) {
  st.push_back(v);
  vis[v] = 1;
  for (int i = 0; i < G[v].size(); i++) {
    int to = G[v][i];
    if (!vis[to]) dfs(to);
  }
}
void dfs2(int v) {
  mk[v] = 1;
  for (int i = 0; i < g[v].size(); i++) {
    int to = g[v][i];
    if (mk[to] == 0)
      dfs2(to);
    else if (mk[to] == 1) {
      flag = 1;
    }
  }
  mk[v] = 2;
}
int main() {
  int n, m, x, y;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    G[i].clear();
    g[i].clear();
  }
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &x, &y);
    G[x].push_back(y);
    G[y].push_back(x);
    g[x].push_back(y);
  }
  int ans = n;
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      flag = 0;
      dfs(i);
      for (int j = 0; j < st.size(); j++) mk[st[j]] = 0;
      for (int j = 0; j < st.size(); j++) {
        int to = st[j];
        if (mk[to] == 0) dfs2(to);
      }
      if (flag == 0) ans--;
      st.clear();
    }
  }
  printf("%d\n", ans);
  return 0;
}
