#include <bits/stdc++.h>
using namespace std;
vector<int> g1[111111], g2[111111], dfn, g3[111111];
int N, M, C[111111], gcnt, gnum, P[111111];
int ans, S[111111], chk;
void dfs(int node) {
  int i;
  C[node] = 1;
  for (i = 0; i < g1[node].size(); i++)
    if (!C[g1[node][i]]) dfs(g1[node][i]);
  dfn.push_back(node);
}
void dfs2(int node) {
  int i;
  C[node] = 1;
  P[node] = gcnt;
  gnum++;
  for (i = 0; i < g2[node].size(); i++)
    if (!C[g2[node][i]]) dfs2(g2[node][i]);
}
void dfs3(int node) {
  int i;
  C[node] = 1;
  gnum++;
  if (S[node]) chk = 1;
  for (i = 0; i < g3[node].size(); i++)
    if (!C[g3[node][i]]) dfs3(g3[node][i]);
}
int main() {
  int i, j, a, b;
  scanf("%d%d", &N, &M);
  for (i = 1; i <= M; i++) {
    scanf("%d%d", &a, &b);
    g1[a].push_back(b);
    g2[b].push_back(a);
  }
  for (i = 1; i <= N; i++)
    if (!C[i]) dfs(i);
  memset(C, 0, sizeof C);
  for (i = dfn.size() - 1; i >= 0; i--) {
    if (!C[dfn[i]]) {
      gcnt++;
      gnum = 0;
      dfs2(dfn[i]);
      if (gnum > 1) {
        ans += gnum - 1;
        S[gcnt] = 1;
      }
    }
  }
  for (i = 1; i <= N; i++) {
    for (j = 0; j < g1[i].size(); j++) {
      if (P[i] == P[g1[i][j]]) continue;
      g3[P[i]].push_back(P[g1[i][j]]);
      g3[P[g1[i][j]]].push_back(P[i]);
    }
  }
  memset(C, 0, sizeof C);
  for (i = 1; i <= gcnt; i++) {
    if (!C[i]) {
      gnum = 0;
      chk = 0;
      dfs3(i);
      if (chk)
        ans += (gnum);
      else
        ans += gnum - 1;
    }
  }
  printf("%d", ans);
  return 0;
}
