/*
食物链 拓扑图DP
*/
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;
const int A = 1e7+10;
const int B = 1e6+10;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
inline int read() {
char c = getchar();
int x = 0, f = 1;
for ( ; !isdigit(c); c = getchar()) if (c == '-') f = -1;
for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
return x * f;
}
struct node{int v,nxt;} e[B];
int rd[B],cd[B],n,m,f[B],cnt,head[B];
void modify(int u,int v)
{
e[++cnt].nxt=head[u];
e[cnt].v=v;
head[u]=cnt;
}
queue<int>q;
int main()
{
int x,y;
n=read(),m=read();
for (int i=1;i<=m;i++)
{
x=read(),y=read();
modify(x,y);
rd[y]++, cd[x]++;
}
for (int i=1;i<=n;i++)
if (!rd[i])
{
if (cd[i]) f[i]=1;
q.push(i);
}
while (!q.empty())
{
//cout<<1<<endl;
int u=q.front();
q.pop();
for (int i=head[u];i;i=e[i].nxt)
{
int v=e[i].v;
f[v]+=f[u];
rd[v]--;
if (!rd[v]) q.push(v);
}
}
int ans=0;
for (int i=1;i<=n;i++) if (!cd[i]) ans+=f[i];
printf("%d",ans);
}