#include <bits/stdc++.h>
using namespace std;
ifstream f("ctc.in");
ofstream g("ctc.out");
vector <int> v[100009], ctc[100009], vt[100009];
int curr=0;
bool viz[100009];
int lin[100009], cnt=0;
void dfs0 (int nod)
{
viz[nod]=1;
for(auto y:v[nod])
{
if (!viz[y])
dfs0(y);
}
lin[++cnt]=nod;
}
void dfs (int nod)
{
ctc[curr].push_back(nod);
viz[nod]=1;
for (auto y:vt[nod])
if (!viz[y]) dfs(y);
}
signed main ()
{
int n, m;
f >> n >> m;
for (int i=1; i<=m; i++)
{
int x, y;
f >> x >> y;
v[x].push_back(y);
vt[y].push_back(x);
}
for (int i=1; i<=n; i++)
{
if (!viz[i])
dfs0 (i);
}
for (int i=1; i<=n; i++)
viz[i]=0;
for (int i=n; i>=1; i--)
{
if (!viz[lin[i]])
curr++, dfs (lin[i]);
}
g<<curr << '\n';
for (int i=1; i<=curr; i++)
{
for (auto y:ctc[i])
g << y <<' ';
g <<'\n';
}
}