#include <bits/stdc++.h>
using namespace std;
const int N = 100 * 1000 + 5;
int n, m, cnt;
short int mark[N];
bool visited[N];
vector<int> in[N], out[N], comp[N];
void input() {
  cin.tie(0);
  cin >> n >> m;
  int v, u;
  for (int i = 0; i < m; i++) {
    cin >> v >> u;
    v--, u--;
    out[v].push_back(u);
    in[u].push_back(v);
  }
  return;
}
void getComp(int v) {
  visited[v] = true;
  for (int u : in[v])
    if (!visited[u]) getComp(u);
  for (int u : out[v])
    if (!visited[u]) getComp(u);
  comp[cnt].push_back(v);
  return;
}
bool isDAG(int v) {
  bool res = true;
  mark[v]++;
  for (int u : out[v])
    if (!mark[u])
      res &= isDAG(u);
    else if (mark[u] == 1)
      return false;
  mark[v]++;
  return res;
}
void solve() {
  for (int i = 0; i < n; i++)
    if (!visited[i]) {
      getComp(i);
      cnt++;
    }
  for (int i = 0; i < cnt; i++) {
    bool check = true;
    for (int v : comp[i])
      if (!mark[v]) check &= isDAG(v);
    if (check) n--;
  }
}
int main() {
  ios::sync_with_stdio(false);
  input();
  solve();
  cout << n << endl;
  return 0;
}
