#include<algorithm>
#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<map>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template<typename T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return 0;}
template<typename T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return 0;}

using P = pair<int, int>;

class SCC {
private:
    int group_num_;
    std::vector<int> groups_;

    void dfs1(const std::vector<std::vector<int>>& graph, std::vector<bool>& visited, int node, std::vector<int>& tsorted_nodes) {
        visited[node] = true;
        for (auto& next_node : graph[node]) {
            if (!visited[next_node]) {
                dfs1(graph, visited, next_node, tsorted_nodes);
            }
        }
        tsorted_nodes.push_back(node);
    }

    void dfs2(const std::vector<std::vector<int>>& graph, int node) {
        groups_[node] = group_num_;
        for (auto& next_node : graph[node]) {
            if (groups_[next_node] < 0) {
                dfs2(graph, next_node);
            }
        }
    }
public:
    SCC(const std::vector<std::vector<int>>& graph, const std::vector<std::vector<int>>& rev_graph) {
        int N = (int)graph.size();
        std::vector<int> tsorted_nodes;
        std::vector<bool> visited(N, false);
        for (int node = 0; node < N; ++node) {
            if (!visited[node]) {
                dfs1(graph, visited, node, tsorted_nodes);
            }
        }
        std::reverse(tsorted_nodes.begin(), tsorted_nodes.end());
        groups_ = std::vector<int>(N, -1);
        group_num_ = 0;
        for (auto& node : tsorted_nodes) {
            if (groups_[node] < 0) {
                dfs2(rev_graph, node);
                ++group_num_;
            }
        }
    }

    int getGroupNum() const {
        return group_num_;
    }

    std::vector<int> getSCC() const {
        return groups_;
    }
};

vector<int> tsort(int N, const vector<vector<int>>& graph) {
    vector<int> in_cnt(N, 0);
    rep(i, 0, N) {
        for (auto& nxt_node : graph[i]) {
            ++in_cnt[nxt_node];
        }
    }
    queue<int> que;
    rep(i, 0, N) {
        if (!in_cnt[i]) {
            que.push(i);
        }
    }
    vector<int> res;
    while (!que.empty()) {
        int node = que.front();
        que.pop();
        res.push_back(node);
        for (auto& nxt_node : graph[node]) {
            --in_cnt[nxt_node];
            if (!in_cnt[nxt_node]) {
                que.push(nxt_node);
            }
        }
    }
    return res;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, M;
    cin >> N >> M;
    vector<vector<int>> graph(N), rev_graph(N);
    vector<int> U(M), V(M);
    rep(i, 0, M) {
        cin >> U[i] >> V[i];
        graph[U[i]].push_back(V[i]);
        rev_graph[V[i]].push_back(U[i]);
    }
    SCC scc(graph, rev_graph);
    int K = scc.getGroupNum();
    vector<int> groups = scc.getSCC();
    map<P, bool> exists;
    vector<vector<int>> G(K);
    rep(i, 0, M) {
        if (groups[U[i]] != groups[V[i]] && !exists[P(groups[U[i]], groups[V[i]])]) {
            G[groups[U[i]]].push_back(groups[V[i]]);
            exists[P(groups[U[i]], groups[V[i]])] = true;
        }
    }
    vector<int> tsorted_nodes = tsort(K, G);
    vector<vector<int>> scc_nodes(K);
    rep(i, 0, N) {
        scc_nodes[groups[i]].push_back(i);
    }
    cout << K << endl;
    rep(i, 0, K) {
        cout << scc_nodes[tsorted_nodes[i]].size() << ' ';
        for (auto& node : scc_nodes[tsorted_nodes[i]]) {
            cout << node << ' ';
        }
        cout << endl;
    }
    return 0;
}

Measured memory_bytes: 138844