#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=40001;
int dfn[N],nex[N],fir[N],low[N];
int to[N],du[N],sd[N],co[N],st[N];
int top,col=0,num,tot,n,m,ans=0,u=0;
inline void add(int x,int y)
{
++tot;
to[tot]=x;
nex[tot]=fir[y];
fir[y]=tot;
}
inline int min(int x,int y)
{
return x<y?x:y;
}
inline void tarjan(int u)
{
dfn[u]=low[u]=++num;
st[++top]=u;
for(int i=fir[u];i;i=nex[i])
{
int v=to[i];
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(!co[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
co[u]=++col;
++sd[col];
while(st[top]!=u)
{
++sd[col];
co[st[top]]=col;
--top;
}
--top;
}
}
signed main()
{
cin>>n>>m;
for(int i=1,x,y;i<=m;i++)
{
cin>>x>>y;
add(x,y);
}
for(int i=1;i<=n;i++)
if(!dfn[i]) tarjan(i);
for(int i=1;i<=n;i++)
for(int j=fir[i];j;j=nex[j])
if(co[i]!=co[to[j]])
du[co[to[j]]]++;
for(int i=1;i<=col;i++)
if(!du[i])
{
u++;
ans=sd[i];
}
if(u==1) cout<<ans;
else cout<<"0";
return 0;
}
/*
3 3
1 2
2 1
2 3
Ans:
1
*/
谢谢各位----