#include <bits/stdc++.h>
using namespace std;
struct Node {
  int key;
  int color;
  Node *l, *r, *p;
};
struct Tree {
  Node *root;
  Node *begin, *end;
  int size;
};
Tree *create_tree() {
  Tree *T = (Tree *)malloc(sizeof(Tree));
  T->root = T->begin = T->end = NULL;
  T->size = 0;
  return T;
}
Node *right_rotate(Tree *T, Node *B) {
  Node *A = B->l;
  B->l = A->r;
  if (B->l) B->l->p = B;
  A->r = B;
  if (!B->p)
    T->root = A;
  else if (B->p->l == B)
    B->p->l = A;
  else
    B->p->r = A;
  A->p = B->p;
  B->p = A;
  return A;
}
Node *left_rotate(Tree *T, Node *A) {
  Node *B = A->r;
  A->r = B->l;
  if (A->r) A->r->p = A;
  B->l = A;
  if (!A->p)
    T->root = B;
  else if (A->p->l == A)
    A->p->l = B;
  else
    A->p->r = B;
  B->p = A->p;
  A->p = B;
  return B;
}
Node *next(Node *u) {
  if (!u) return NULL;
  if (u->r) {
    u = u->r;
    while (u->l) u = u->l;
    return u;
  } else {
    while (u->p && u->p->r == u) u = u->p;
    return u->p;
  }
}
Node *prev(Node *u) {
  if (!u) return NULL;
  if (u->l) {
    u = u->l;
    while (u->r) u = u->r;
    return u;
  } else {
    while (u->p && u->p->l == u) u = u->p;
    return u->p;
  }
}
int count(Tree *T, int key) {
  Node *u = T->root;
  while (u) {
    if (key < u->key)
      u = u->l;
    else if (key > u->key)
      u = u->r;
    else
      return 1;
  }
  return 0;
}
bool show;
void insert(Tree *T, int key) {
  Node *x = T->root, *par = NULL;
  while (x) {
    par = x;
    if (key < x->key)
      x = x->l;
    else if (key > x->key)
      x = x->r;
    else
      return;
  }
  x = (Node *)malloc(sizeof(Node));
  x->l = x->r = NULL;
  x->p = par;
  if (!x->p)
    T->root = T->begin = T->end = x;
  else if (key < x->p->key) {
    x->p->l = x;
    if (T->begin == x->p) T->begin = x;
  } else {
    x->p->r = x;
    if (T->end == x->p) T->end = x;
  }
  x->key = key;
  x->color = 1;
  T->size++;
  while (x->p != NULL && x->p->color == 1) {
    if (x->p == x->p->p->l) {
      Node *y = x->p->p->r;
      if (y && y->color == 1) {
        x->p->color = 0;
        y->color = 0;
        x->p->p->color = 1;
        x = x->p->p;
      } else {
        if (x == x->p->r) {
          x = x->p;
          left_rotate(T, x);
        }
        x->p->color = 0;
        x->p->p->color = 1;
        right_rotate(T, x->p->p);
      }
    } else {
      Node *y = x->p->p->l;
      if (y && y->color == 1) {
        x->p->color = 0;
        y->color = 0;
        x->p->p->color = 1;
        x = x->p->p;
      } else {
        if (x == x->p->l) {
          x = x->p;
          right_rotate(T, x);
        }
        x->p->color = 0;
        x->p->p->color = 1;
        left_rotate(T, x->p->p);
      }
    }
  }
  T->root->color = 0;
}
void clear(Node *u) {
  Node *tmp;
  while (u) {
    while (u->l != NULL || u->r != NULL) {
      u = u->l ? u->l : u->r;
    }
    tmp = u->p;
    if (tmp) {
      if (tmp->l == u)
        tmp->l = NULL;
      else
        tmp->r = NULL;
    }
    free(u);
    u = tmp;
  }
}
Tree *delete_tree(Tree *T) {
  if (T) {
    clear(T->begin);
    free(T);
  }
  return NULL;
}
void dfs(Node *u) {
  if (!u) return;
  printf("%p, L = %p, R = %p; key = %d; color = %d\n", u, u->l, u->r, u->key,
         u->color);
  dfs(u->l);
  dfs(u->r);
}
void Delete_fix(Tree *T, Node *x) {
  while (x->p != NULL && x->color == 0) {
    if (x == x->p->l) {
      Node *w = x->p->r;
      if (w->color == 1) {
        w->color = 0;
        x->p->color = 1;
        left_rotate(T, x->p);
        w = x->p->r;
      }
      if ((!w->r || w->r->color == 0) && (!w->l || w->l->color == 0)) {
        w->color = 1;
        x = x->p;
      } else {
        if (w->l && w->l->color == 1) {
          w->l->color = 0;
          w->color = 1;
          right_rotate(T, w);
          w = x->p->r;
        }
        w->color = x->p->color;
        x->p->color = 0;
        w->r->color = 0;
        left_rotate(T, x->p);
        x = T->root;
      }
    } else {
      Node *w = x->p->l;
      if (w->color == 1) {
        w->color = 0;
        x->p->color = 1;
        right_rotate(T, x->p);
        w = x->p->l;
      }
      if ((!w->r || w->r->color == 0) && (!w->l || w->l->color == 0)) {
        w->color = 1;
        x = x->p;
      } else {
        if (w->r && w->r->color == 1) {
          w->r->color = 0;
          w->color = 1;
          left_rotate(T, w);
          w = x->p->l;
        }
        w->color = x->p->color;
        x->p->color = 0;
        w->l->color = 0;
        right_rotate(T, x->p);
        x = T->root;
      }
    }
  }
  x->color = 0;
}
void Delete(Tree *T, Node *z) {
  Node *y = z, *x = NULL, *tmp;
  int y_original_color = y->color;
  if (T->begin == z) T->begin = next(T->begin);
  if (T->end == z) T->end = prev(T->end);
  T->size--;
  if (z->l != NULL && z->r != NULL) {
    y = z->r;
    while (y->l) y = y->l;
    int tmp_key = z->key;
    z->key = y->key;
    y->key = tmp_key;
    z = y;
  }
  if (z->color == 0) {
    Delete_fix(T, z);
  }
  if (z->l != NULL) {
    if (z->p) {
      if (z->p->l == z)
        z->p->l = z->l;
      else
        z->p->r = z->l;
    } else
      T->root = z->l;
    z->l->p = z->p;
  } else if (z->r != NULL) {
    if (z->p) {
      if (z->p->l == z)
        z->p->l = z->r;
      else
        z->p->r = z->r;
    } else
      T->root = z->r;
    z->r->p = z->p;
  } else {
    if (z->p) {
      if (z->p->l == z)
        z->p->l = NULL;
      else
        z->p->r = NULL;
    } else
      T->root = NULL;
  }
  free(z);
  if (T->root) T->root->color = 0;
}
void Delete_val(Tree *T, int val) {
  Node *u = T->root;
  while (u) {
    if (val < u->key)
      u = u->l;
    else if (val > u->key)
      u = u->r;
    else
      break;
  }
  if (u) Delete(T, u);
}
const int N = 300005;
vector<int> g[N], gt[N];
int n, m;
vector<int> topo;
int vis[N];
int cnt[N];
int alm[N];
int deg[N], in[N];
int fine[N];
void dfs(int u) {
  vis[u] = 1;
  for (int v : g[u])
    if (!vis[v]) dfs(v);
  topo.push_back(u);
}
int check(Node *u) {
  if (!u) return 1;
  int a = check(u->l);
  int b = check(u->r);
  if (a != b) {
    return -1;
    exit(0);
  }
  if (u->color == 0) return a + 1;
  return a;
}
int main() {
  Tree *s = create_tree();
  scanf("%d %d", &n, &m);
  for (int i = 0, a, b; i < m; i++) {
    scanf("%d %d", &a, &b);
    g[a].push_back(b);
    gt[b].push_back(a);
  }
  for (int i = 1; i <= n; i++)
    if (!vis[i]) dfs(i);
  reverse(topo.begin(), topo.end());
  for (int i = 0; i < n; i++) {
    int u = topo[i];
    for (int v : gt[u]) {
      deg[v]++;
      if (count(s, v)) {
        Delete_val(s, v);
      }
      if (deg[v] == 2) {
        for (int k : g[v]) fine[k]++;
      }
    }
    if (s->size == 1) {
      alm[u] = 1;
      if ((int)gt[s->begin->key].size() > fine[s->begin->key]) alm[u] = 0;
    } else if (!s->size)
      cnt[u] = 1;
    insert(s, u);
  }
  memset(deg, 0, sizeof deg);
  memset(fine, 0, sizeof fine);
  reverse(topo.begin(), topo.end());
  delete_tree(s);
  s = create_tree();
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int u = topo[i];
    for (int v : g[u]) {
      deg[v]++;
      if (count(s, v)) {
        Delete_val(s, v);
      }
      if (deg[v] == 2) {
        for (int k : gt[v]) fine[k]++;
      }
    }
    bool tmp;
    if (s->size == 1) {
      tmp = 1;
      if ((int)g[s->begin->key].size() > fine[s->begin->key]) tmp = 0;
      if (tmp && cnt[u]) ans++;
    } else if (!s->size && (cnt[u] + alm[u]))
      ans++;
    insert(s, u);
  }
  printf("%d\n", ans);
  delete_tree(s);
}
