#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1E5 + 5;
int n, m;
bool foundCir, mark[MAXN], inPath[MAXN];
vector<int> adj[MAXN], adj2[MAXN], st;
void go1(int u) {
  st.push_back(u);
  mark[u] = 1;
  for (int i = 0; i < int(adj2[u].size()); ++i) {
    int v = adj2[u][i];
    if (!mark[v]) go1(v);
  }
}
void go2(int u) {
  mark[u] = 1;
  inPath[u] = 1;
  for (int i = 0; i < int(adj[u].size()); ++i) {
    int v = adj[u][i];
    if (!mark[v])
      go2(v);
    else if (inPath[v])
      foundCir = 1;
  }
  inPath[u] = 0;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0, _n = m; i < _n; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    adj[u].push_back(v);
    adj2[u].push_back(v);
    adj2[v].push_back(u);
  }
  int res = 0;
  for (int u = 1; u <= n; ++u)
    if (!mark[u]) {
      st.clear();
      go1(u);
      for (int i = 0; i < int(st.size()); ++i) mark[st[i]] = 0;
      foundCir = 0;
      for (int i = 0; i < int(st.size()); ++i)
        if (!mark[st[i]]) go2(st[i]);
      res += st.size();
      if (!foundCir) --res;
    }
  printf("%d", res);
}
