#include <fstream>
#include <vector>
using namespace std;
ifstream in("ctc.in");
ofstream out("ctc.out");
int n, m, st[100005], nctc;
bool viz_g[100005], viz_gt[100005];
vector<int> g[100005];
vector<int> gt[100005]; // graful transpus
vector<int> ctc[100005];
void citire();
void dfs_g(int x);
void dfs_gt(int x);
int main() {
citire();
// "Sortare topologica"
for (int i = 1; i <= n; ++i) {
if (!viz_g[i]) dfs_g(i);
}
// DFS pe graful transpus in ordine topologica
for (int i = n; i >= 1; --i) {
if (!viz_gt[st[i]]) {
nctc++;
dfs_gt(st[i]);
}
}
out << nctc << '\n';
for (int i = 1; i <= nctc; ++i) {
for (int j = 0; j < ctc[i].size(); ++j)
out << ctc[i][j] << " ";
out << '\n';
}
return 0;
}
void dfs_gt(int x) {
viz_gt[x] = true;
for (int i = 0; i < gt[x].size(); ++i) {
int y = gt[x][i];
if (!viz_gt[y]) dfs_gt(y);
}
ctc[nctc].push_back(x);
}
void dfs_g(int x) {
viz_g[x] = true;
for (int i = 0; i < g[x].size(); ++i) {
int y = g[x][i];
if (!viz_g[y]) dfs_g(y);
}
st[++st[0]] = x;
}
void citire() {
in >> n >> m;
for (int i = 1; i <= m; ++i) {
int x, y;
in >> x >> y;
g[x].push_back(y);
gt[y].push_back(x);
}
}