#include <fstream>
#include <vector>
using namespace std;
ifstream fin("ctc.in");
ofstream fout("ctc.out");
int n, m, x, y, k, nrc;
vector<int> v[100001], vt[100001], comp[100001];
bool viz[100001], viz2[100001];
int ord[100001];
void dfs1(int nod)
{
viz[nod] = 1;
for (int i = 0; i < v[nod].size(); i++)
if (viz[v[nod][i]] == 0)
dfs1(v[nod][i]);
k++;
ord[k] = nod;
}
void dfs2(int nod)
{
viz2[nod] = 1;
comp[nrc].push_back(nod);
for (int i = 0; i < vt[nod].size(); i++)
if (viz2[vt[nod][i]] == 0)
dfs2(vt[nod][i]);
}
int main()
{
fin >> n >> m;
for (int i = 1; i <= m; i++)
{
fin >> x >> y;
v[x].push_back(y);
vt[y].push_back(x);
}
for (int i = 1; i <= n; i++)
if (viz[i] == 0)
dfs1(i);
for (int i = n; i >= 1; i--)
if (viz2[ord[i]] == 0)
{
nrc++;
dfs2(ord[i]);
}
fout << nrc << '\n';
for (int i = 1; i <= nrc; i++)
{
for (int j = 0; j < comp[i].size(); j++)
fout << comp[i][j] << ' ';
fout << '\n';
}
return 0;
}