#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream fin("ctc.in");
ofstream fout("ctc.out");
const int NMAX = 100001;
int n, m;
vector<int> graf[NMAX];
vector<int> grafInv[NMAX];
bool viz[NMAX];
vector<int> ordine;
vector<vector<int> > componente;
void dfs1(int start)
{
stack<pair<int, int> > st;
viz[start] = true;
st.push(make_pair(start, 0));
while (!st.empty())
{
int nod = st.top().first;
int &poz = st.top().second;
if (poz < (int)graf[nod].size())
{
int vecin = graf[nod][poz];
poz++;
if (!viz[vecin])
{
viz[vecin] = true;
st.push(make_pair(vecin, 0));
}
}
else
{
ordine.push_back(nod);
st.pop();
}
}
}
void dfs2(int start)
{
stack<int> st;
vector<int> componenta;
st.push(start);
viz[start] = true;
while (!st.empty())
{
int nod = st.top();
st.pop();
componenta.push_back(nod);
for (int i = 0; i < (int)grafInv[nod].size(); i++)
{
int vecin = grafInv[nod][i];
if (!viz[vecin])
{
viz[vecin] = true;
st.push(vecin);
}
}
}
componente.push_back(componenta);
}
int main()
{
fin >> n >> m;
for (int i = 1; i <= m; i++)
{
int x, y;
fin >> x >> y;
graf[x].push_back(y);
grafInv[y].push_back(x);
}
for (int i = 1; i <= n; i++)
{
if (!viz[i])
dfs1(i);
}
for (int i = 1; i <= n; i++)
viz[i] = false;
for (int i = (int)ordine.size() - 1; i >= 0; i--)
{
int nod = ordine[i];
if (!viz[nod])
dfs2(nod);
}
fout << componente.size() << '\n';
for (int i = 0; i < (int)componente.size(); i++)
{
for (int j = 0; j < (int)componente[i].size(); j++)
fout << componente[i][j] << " ";
fout << '\n';
}
fin.close();
fout.close();
return 0;
}