#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
ifstream fin("ctc.in");
ofstream fout("ctc.out");
const int MAXN = 100005;
vector<int> adj[MAXN];
int tin[MAXN], low[MAXN];
bool in_stack[MAXN];
stack<int> st;
int timer;
vector<vector<int>> scc;
void dfs(int u) {
tin[u] = low[u] = ++timer;
st.push(u);
in_stack[u] = true;
for (int v : adj[u]) {
if (!tin[v]) {
dfs(v);
low[u] = min(low[u], low[v]);
} else if (in_stack[v]) {
low[u] = min(low[u], tin[v]);
}
}
if (low[u] == tin[u]) {
vector<int> current_scc;
while (true) {
int v = st.top();
st.pop();
in_stack[v] = false;
current_scc.push_back(v);
if (u == v) break;
}
scc.push_back(current_scc);
}
}
int main() {
int n, m;
if (!(fin >> n >> m)) return 0;
for (int i = 0; i < m; ++i) {
int u, v;
fin >> u >> v;
adj[u].push_back(v);
}
for (int i = 1; i <= n; ++i) {
if (!tin[i]) {
dfs(i);
}
}
fout << scc.size() << "\n";
for (const auto& comp : scc) {
for (int node : comp) {
fout << node << " ";
}
fout << "\n";
}
return 0;
}