#include <bits/stdc++.h>
using namespace std;
const long long N = 100005;
long long n, x;
vector<long long> g[N];
bool vis[N];
bool good[N];
long long cnt;
void ADD_EDGE(long long u, long long v) { g[u].push_back(v); }
void dfs1(long long v) {
  good[v] = true;
  for (auto to : g[v])
    if (!good[to]) dfs1(to);
}
void dfs2(long long v) {
  vis[v] = true;
  ++cnt;
  for (auto to : g[v])
    if (!vis[to] && !good[to]) dfs2(to);
}
long long Minimum_Edges() {
  dfs1(x);
  vector<pair<long long, long long> > val;
  for (long long i = 0; i < n; ++i) {
    if (!good[i]) {
      cnt = 0;
      memset(vis, false, sizeof(vis));
      dfs2(i);
      val.push_back(make_pair(cnt, i));
    }
  }
  sort(val.begin(), val.end());
  reverse(val.begin(), val.end());
  long long ans = 0;
  for (auto it : val) {
    if (!good[it.second]) {
      ++ans;
      dfs1(it.second);
    }
  }
  return ans;
}
signed main() {
  long long M;
  cin >> n >> M;
  long long a, b;
  while (M--) {
    cin >> a >> b;
    x = a;
    ADD_EDGE(a, b);
  }
  cout << Minimum_Edges();
  return 0;
}
