#include<bits/stdc++.h>
#define MAX 5005
using namespace std;
int n,m,low[MAX],dfn[MAX],top=0,dnum=0,sta[MAX],howmany=0,color[MAX];
bool vis[MAX]={0};
vector<int>G[MAX];
void Tarjan(int u)
{
dfn[u]=++dnum;
vis[u]=true;
low[u]=dnum;
sta[++top]=u;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
if(!dfn[v])
{
Tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(vis[v])low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
vis[u]=false;
color[u]=++howmany;
while(sta[top]!=u)
{
color[sta[top]]=howmany;
vis[sta[top--]]=false;
}
top--;
}
}
int main()
{
int a,b;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
G[a].push_back(b);
}
Tarjan(1);
printf("%d\n",howmany);
for(int i=1;i<=n;i++)cout<<i<<" "<<color[i]<<endl;
return 0;
}