#include<iostream>
#include<cstring>
using namespace std;
const int M = 1e5 + 7;
const int N = 1e5 + 7;
struct node {
int v;
int nxt;
}e[M];
int head[N], cnt;
int p[N], st[N], id, top, scc;
int dfn[N], low[N], belong[N];
void add(int u, int v)
{
e[cnt].v = v, e[cnt].nxt = head[u];
head[u] = cnt++;
}
void init()
{
memset(head, -1, sizeof(head));
memset(p, 0, sizeof(p));
memset(dfn, 0, sizeof(dfn));
id = top = cnt = 0;
}
void dfs(int u)
{
dfn[u] = low[u] = ++id;
st[++top] = u, p[u] = 1;
int v;
for (int i = head[u]; i != -1; i = e[i].nxt)
{
v = e[i].v;
if (!dfn[v])
{
dfs(v);
if (low[v] < low[u])
low[u] = low[v];
}
else if (p[v] && dfn[v] < low[u])
{
low[u] = dfn[v];
}
}
if (dfn[u] == low[u])
{
++scc;
do {
v = st[top--];
p[v] = 0;
belong[v] = scc;
} while (v != u);
}
}
void Tarjian(int n)
{
for (int i = 1; i <= n; i++)
{
if (!dfn[i])
dfs(i);
}
cout << scc << endl;
for (int i = 1; i <= n; i++)
{
cout << i << " " << belong[i] << endl;
}
}
int main()
{
int n, m, u, v;
cin >> n >> m;
init();
while (m--)
{
cin >> u >> v;
add(u, v);
}
Tarjian(n);
return 0;
}