#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
const int BUF_SIZE = 4096;
char buf[BUF_SIZE];
int posBuf = BUF_SIZE;
FILE *fin;
inline int readInt() {
while (posBuf < BUF_SIZE && buf[posBuf] <= ' ') {
posBuf++;
}
if (posBuf == BUF_SIZE) {
fread(buf, 1, BUF_SIZE, fin);
posBuf = 0;
while (posBuf < BUF_SIZE && buf[posBuf] <= ' ') {
posBuf++;
}
}
int res = 0;
while (posBuf < BUF_SIZE && buf[posBuf] >= '0' && buf[posBuf] <= '9') {
res = res * 10 + (buf[posBuf] - '0');
posBuf++;
if (posBuf == BUF_SIZE) {
fread(buf, 1, BUF_SIZE, fin);
posBuf = 0;
}
}
return res;
}
const int MAXN = 100005;
vector<int> adj[MAXN];
int idx[MAXN], low[MAXN], timer;
bool in_stack[MAXN];
stack<int> st;
vector<vector<int>> ctc;
void tarjan(int u) {
idx[u] = low[u] = ++timer;
st.push(u);
in_stack[u] = true;
for (int v : adj[u]) {
if (!idx[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
} else if (in_stack[v]) {
low[u] = min(low[u], idx[v]);
}
}
if (low[u] == idx[u]) {
vector<int> current_comp;
while (true) {
int v = st.top();
st.pop();
in_stack[v] = false;
current_comp.push_back(v);
if (u == v) break;
}
ctc.push_back(current_comp);
}
}
int main() {
fin = fopen("ctc.in", "r");
ofstream fout("ctc.out");
if (!fin) return 0;
int N = readInt();
int M = readInt();
for (int i = 0; i < M; ++i) {
int u = readInt();
int v = readInt();
adj[u].push_back(v);
}
for (int i = 1; i <= N; ++i) {
if (!idx[i]) {
tarjan(i);
}
}
fout << ctc.size() << "\n";
for (const auto& comp : ctc) {
for (size_t i = 0; i < comp.size(); ++i) {
fout << comp[i] << (i + 1 == comp.size() ? "" : " ");
}
fout << "\n";
}
fclose(fin);
fout.close();
return 0;
}