#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:26777216")
const int mod = 2e9 + 7;
const double eps = 1e-8;
const int N = 803;
const double pi = acos(-1.0);
const long long inf = 1ll << 50;
int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
struct union_set {
  int fa[103];
  void init(int n) {
    for (int i = 0; i <= n; i++) fa[i] = i;
    return;
  }
  int getroot(int i) {
    if (fa[i] != i) fa[i] = getroot(fa[i]);
    return fa[i];
  }
  void _union(int i, int j) { fa[i] = j; }
};
int pow_mod(int a, int b, int c) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = ans * a % c;
    a = 1ll * a * a % c;
    b >>= 1;
  }
  return ans % c;
}
const int MN = 100003;
vector<int> edge[MN];
vector<int> SCC[MN];
stack<int> s;
int low[MN], dfn[MN];
int instack[MN], stap[MN];
int belong[MN];
int num[MN];
int ind[MN];
int tp, p;
int bcnt;
void tarjan(int x) {
  low[x] = dfn[x] = ++tp;
  stap[++p] = x;
  instack[x] = 1;
  for (int i = 0; i < edge[x].size(); i++) {
    int v = edge[x][i];
    if (dfn[v] == -1) {
      tarjan(v);
      if (low[x] > low[v]) low[x] = low[v];
    } else if (instack[v] && dfn[v] < low[x])
      low[x] = dfn[v];
  }
  if (low[x] == dfn[x]) {
    int top;
    do {
      top = stap[p];
      belong[top] = bcnt;
      instack[top] = 0;
      p--;
    } while (x != top);
    bcnt++;
  }
}
int hsize[MN] = {0};
int have[MN];
int rhave[MN];
int id[MN], top = 1;
void dfs(int u, int l) {
  if (id[u] == l) return;
  id[u] = l;
  have[l]++;
  rhave[l] += hsize[u];
  for (int v = 0; v < SCC[u].size(); v++) dfs(SCC[u][v], l);
  return;
}
int main() {
  int n, m, i, a, b, j;
  bcnt = tp = p = 0;
  scanf("%d%d", &n, &m);
  memset(dfn, -1, sizeof(dfn));
  memset(low, 0, sizeof(low));
  memset(instack, 0, sizeof(instack));
  memset(belong, 0, sizeof(belong));
  memset(ind, 0, sizeof(ind));
  for (i = 1; i <= n; i++) {
    edge[i].clear();
    SCC[i].clear();
  }
  for (i = 0; i < m; i++) {
    scanf("%d%d", &a, &b);
    edge[a].push_back(b);
  }
  for (i = 1; i <= n; i++) {
    if (dfn[i] == -1) tarjan(i);
  }
  for (i = 1; i <= n; i++) {
    hsize[belong[i]]++;
    for (j = 0; j < edge[i].size(); j++) {
      int x = belong[i];
      int y = belong[edge[i][j]];
      if (x != y) {
        SCC[x].push_back(y);
        SCC[y].push_back(x);
      }
    }
  }
  for (int i = 0; i < bcnt; i++)
    if (id[i] == 0) dfs(i, top++);
  int ans = 0;
  for (int i = 1; i <= bcnt; i++)
    if (have[i]) ans += (have[i] == rhave[i]) ? rhave[i] - 1 : rhave[i];
  cout << ans;
  return 0;
}
