#include <bits/stdc++.h>
using namespace std;
vector<int> E[100001];
vector<int> G[100001];
int n, m, co = 1, res;
int vis[100001], col[100001], cnt[100001], ans[100001];
void dfs1(int x, int c) {
  col[x] = c;
  cnt[c]++;
  for (int i = 0; i < E[x].size(); i++)
    if (!col[E[x][i]]) dfs1(E[x][i], c);
}
void dfs2(int x) {
  vis[x] = 1;
  for (int i = 0; i < G[x].size(); i++) {
    if (vis[G[x][i]] == 1) ans[col[x]] = 0;
    if (!vis[G[x][i]]) dfs2(G[x][i]);
  }
  vis[x] = 2;
}
int main() {
  cin >> n >> m;
  memset(ans, -1, sizeof(ans));
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    E[a].push_back(b);
    E[b].push_back(a);
    G[a].push_back(b);
  }
  for (int i = 1; i <= n; i++)
    if (!col[i]) dfs1(i, co++);
  for (int i = 1; i <= n; i++)
    if (!vis[i]) dfs2(i);
  for (int i = 1; i < co; i++) res += cnt[i] + ans[i];
  cout << res << endl;
  return 0;
}
