#include <bits/stdc++.h>
using namespace std;
int N, M, C, cycle, stat[100011];
vector<int> id[100011];
vector<int> adj[100011];
vector<int> adj2[100011];
void dfs(int x) {
  id[C].push_back(x);
  stat[x] = C;
  for (int i = 0; i < (int)adj2[x].size(); i++)
    if (!stat[adj2[x][i]]) dfs(adj2[x][i]);
}
void dfs2(int x) {
  stat[x] = 1;
  for (int i = 0; i < (int)adj[x].size(); i++) {
    if (!stat[adj[x][i]])
      dfs2(adj[x][i]);
    else if (stat[adj[x][i]] == 1)
      cycle = 1;
  }
  stat[x] = 2;
}
int main() {
  if (fopen("data.in", "r")) freopen("data.in", "r", stdin);
  scanf("%d", &N);
  scanf("%d", &M);
  for (int i = 0; i < M; i++) {
    int a, b;
    scanf("%d", &a);
    scanf("%d", &b);
    adj[a].push_back(b);
    adj2[a].push_back(b);
    adj2[b].push_back(a);
  }
  for (int i = 1; i <= N; i++)
    if (!stat[i]) {
      C++;
      dfs(i);
    }
  memset(stat, 0, sizeof(stat));
  int ans = 0;
  for (int i = 1; i <= C; i++) {
    cycle = 0;
    for (int j = 0; j < (int)id[i].size(); j++)
      if (!stat[id[i][j]]) dfs2(id[i][j]);
    ans += id[i].size() - (!cycle);
  }
  printf("%d\n", ans);
}
