#include <bits/stdc++.h>
using namespace std;
bool revisit = 0;
vector<vector<int> > adj, adj2;
vector<int> vis, color;
int col = 1;
int dfs(int v, vector<vector<int> > *list, int c) {
  vis[v] = 1;
  if (c) color[v] = col;
  int cnt = 1;
  for (int i = 0; i < (*list)[v].size(); ++i) {
    int to = (*list)[v][i];
    if (vis[to] == 0) {
      cnt += dfs(to, list, c);
    } else if (vis[to] == 1)
      revisit = 1;
  }
  vis[v] = 2;
  return cnt;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> num(n);
  vis = color = vector<int>(n);
  adj = adj2 = vector<vector<int> >(n);
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    adj[a - 1].push_back(b - 1);
    adj2[a - 1].push_back(b - 1);
    adj2[b - 1].push_back(a - 1);
  }
  for (int i = 0; i < n; ++i) {
    if (!vis[i]) {
      num[col] = dfs(i, &adj2, 1);
      col++;
    }
  }
  vector<int> cc(col + 1, 0);
  fill(vis.begin(), vis.end(), 0);
  int count = 0;
  for (int i = 0; i < n; ++i) {
    revisit = 0;
    if (!vis[i]) {
      dfs(i, &adj, 0);
      if (revisit) cc[color[i]] = 1;
    }
  }
  for (int i = 1; i < col; ++i) {
    if (cc[i])
      count += num[i];
    else
      count += num[i] - 1;
  }
  cout << count << "\n";
  return 0;
}
