#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("ctc.in");
ofstream fout("ctc.out");
const int MAXN = 100005;
vector<int> g[MAXN], gt[MAXN];
vector<int> ordine;
vector<vector<int>> componente;
bool viz[MAXN];
int poz[MAXN];
void dfs1(int start) {
vector<int> stiva;
stiva.push_back(start);
viz[start] = true;
while (!stiva.empty()) {
int x = stiva.back();
if (poz[x] < (int)g[x].size()) {
int y = g[x][poz[x]];
poz[x]++;
if (!viz[y]) {
viz[y] = true;
stiva.push_back(y);
}
} else {
ordine.push_back(x);
stiva.pop_back();
}
}
}
void dfs2(int start) {
vector<int> stiva, comp;
stiva.push_back(start);
viz[start] = true;
while (!stiva.empty()) {
int x = stiva.back();
stiva.pop_back();
comp.push_back(x);
for (int y : gt[x]) {
if (!viz[y]) {
viz[y] = true;
stiva.push_back(y);
}
}
}
componente.push_back(comp);
}
int main() {
int n, m;
fin >> n >> m;
for (int i = 1; i <= m; i++) {
int x, y;
fin >> x >> y;
g[x].push_back(y);
gt[y].push_back(x);
}
// Prima parcurgere DFS
for (int i = 1; i <= n; i++) {
if (!viz[i])
dfs1(i);
}
// Resetam vectorul de vizitare
for (int i = 1; i <= n; i++)
viz[i] = false;
// A doua parcurgere DFS
for (int i = (int)ordine.size() - 1; i >= 0; i--) {
int x = ordine[i];
if (!viz[x])
dfs2(x);
}
fout << componente.size() << '\n';
for (auto &comp : componente) {
for (int x : comp)
fout << x << ' ';
fout << '\n';
}
return 0;
}
//
// Created by daria on 11/8/2026.
//