#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<stack>
using namespace std;
const int maxn = 10000 + 10;
vector<int> G[maxn], G2[maxn];
vector<int> S;
int vis[maxn], sccno[maxn], scc_cnt;
int max(int a,int b)
{
if(a>b) return a;
else return b;
}
void dfs1(int u)
{
if(vis[u]) return;
vis[u] = 1;
for(int i = 0; i < G[u].size(); i++) dfs1(G[u][i]);
S.push_back(u);
}
void dfs2(int u)
{
if(sccno[u]) return;
sccno[u] = scc_cnt;
for(int i = 0; i < G2[u].size(); i++) dfs2(G2[u][i]);
}
void find_scc(int n)
{
int i;
scc_cnt = 0;
S.clear();
memset(sccno, 0, sizeof(sccno));
memset(vis, 0, sizeof(vis));
for(i = 0; i < n; i++) dfs1(i);
for(i = n-1; i >= 0; i--)
if(!sccno[S[i]]) { scc_cnt++; dfs2(S[i]); }
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
int x,y,i,j,v;
for(i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
x--;y--;
G[x].push_back(y);
G2[y].push_back(x);
}
int out[maxn];
memset(out,0,sizeof(out));
find_scc(n);
int cnt=0,ans=0,ob;
for(i=0;i<n;i++)
{
for(j=0;j<G[i].size();j++)
{
v=G[i][j];
if(sccno[i]!=sccno[v])
out[sccno[i]]++;
}
}
for(i=1;i<=scc_cnt;i++)
if(out[i]==0)
{
ob=i;
cnt++;
}
if(cnt>1) printf("0\n");
else
{
for(i=0;i<n;i++)
{
if(sccno[i]==ob)
{
ans++;
// printf("%d ",i);
}
}
printf("%d\n",ans);
}
}
return 0;
}