#include <algorithm>
#include <cstdio> using namespace std; const int N();
int n,m,v,u;
int edgesum,head[N]; struct Edge
{
int from,to,next;
Edge(int from=,int to=,int next=) :
from(from),to(to),next(next) {}
}edge[N];
int ins(int from,int to)
{
edge[++edgesum]=Edge(from,to,head[from]);
return head[from]=edgesum;
} int dfn[N],tim,low[N],vis[N];
int Stack[N],top,instack[N];
int col[N],colsum; void DFS(int now)
{
dfn[now]=low[now]=++tim; vis[now]=;
Stack[++top]=now; instack[now]=;
for(int i=head[now];i;i=edge[i].next)
{
int to=edge[i].to;
if(instack[to]) low[i]=min(low[i],dfn[to]);
else if(!vis[to])
DFS(to),low[i]=min(low[i],low[to]);
}
if(low[now]==dfn[now])
{
colsum++;
col[now]=colsum;
for(;Stack[top]!=now;top--)
{
col[Stack[top]]=colsum;
instack[Stack[top]]=;
}
instack[now]=;
top--;
}
} int main()
{
scanf("%d%d",&n,&m);
for(;m--;)
{
scanf("%d%d",&u,&v);
ins(u,v);
}
for(int i=;i<=n;i++)
if(!vis[i]) DFS(i);
return ;
}