#include <bits/stdc++.h>
using namespace std;
int main() {
ifstream fin("ctc.in");
ofstream fout("ctc.out");
int N, M;
fin >> N >> M;
vector<vector<int>> graf(N + 1), graf_transpus(N + 1);
for (int i = 0; i < M; ++i) {
int x, y;
fin >> x >> y;
graf[x].push_back(y);
graf_transpus[y].push_back(x);
}
vector<bool> vizitat(N + 1, false);
vector<int> ordine;
for (int start = 1; start <= N; ++start) {
if (vizitat[start]) {
continue;
}
vector<pair<int, int>> stiva;
stiva.push_back({start, 0});
vizitat[start] = true;
while (!stiva.empty()) {
int nod = stiva.back().first;
int& urmatoarea_muchie = stiva.back().second;
if (urmatoarea_muchie < static_cast<int>(graf[nod].size())) {
int vecin = graf[nod][urmatoarea_muchie];
++urmatoarea_muchie;
if (!vizitat[vecin]) {
vizitat[vecin] = true;
stiva.push_back({vecin, 0});
}
} else {
ordine.push_back(nod);
stiva.pop_back();
}
}
}
fill(vizitat.begin(), vizitat.end(), false);
vector<vector<int>> componente;
for (int i = N - 1; i >= 0; --i) {
int start = ordine[i];
if (vizitat[start]) {
continue;
}
vector<int> componenta;
vector<int> stiva = {start};
vizitat[start] = true;
while (!stiva.empty()) {
int nod = stiva.back();
stiva.pop_back();
componenta.push_back(nod);
for (int vecin : graf_transpus[nod]) {
if (!vizitat[vecin]) {
vizitat[vecin] = true;
stiva.push_back(vecin);
}
}
}
componente.push_back(componenta);
}
fout << componente.size() << '\n';
for (const vector<int>& componenta : componente) {
for (int nod : componenta) {
fout << nod << ' ';
}
fout << '\n';
}
return 0;
}