#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
#include <bitset>

using namespace std;
using lint = long long int;
long long int INF = 1001001001001001LL;
int inf = 1000000007;
long long int MOD = 1000000007LL;
double PI = 3.1415926535897932;

template<typename T1,typename T2>inline void chmin(T1 &a,const T2 &b){if(a>b) a=b;}
template<typename T1,typename T2>inline void chmax(T1 &a,const T2 &b){if(a<b) a=b;}

#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()

/* do your best */

struct StronglyConnectedComponents {
public:
  const int n;
  vector<bool> isthrough;
  vector<int> vs, cmp;
  vector< vector<int> > G, rG, H; // グラフ、逆辺グラフ、縮約後のグラフ

  StronglyConnectedComponents(vector<vector<int>> &g) :
    n(g.size()), isthrough(n, false), cmp(n, 0), G(n), rG(n) {
    for(int i = 0; i < n; i++) {
      for(size_t j = 0; j < g[i].size(); j++) {
        G[i].push_back(g[i][j]);
        rG[g[i][j]].push_back(i);
      }
    }
  }

  void SccDfsOne(int cur) {
    isthrough[cur] = true;
    for(size_t i = 0; i < G[cur].size(); i++) {
      if(!isthrough[G[cur][i]]) {
        SccDfsOne(G[cur][i]);
      }
    }
    vs.push_back(cur);
  }

  void SccDfsTwo(vector<int> &vec, int cur, int k) {
    cmp[cur] = k;
    isthrough[cur] = true;
    vec.push_back(cur);
    for(size_t i = 0; i < rG[cur].size(); i++) {
      if(!isthrough[rG[cur][i]]) {
        SccDfsTwo(vec, rG[cur][i], k);
      }
    }
  }

  // 縮約後のグループ、グループ数
  pair<vector<int>, int> scc() {
    // 1回めのDFS
    for(int i = 0; i < n; i++)
      if(!isthrough[i]) SccDfsOne(i);

    fill(isthrough.begin(), isthrough.end(), false);
    reverse(vs.begin(), vs.end());
    int k = 0; vector<vector<int>> S;

    // 2回めのDFS
    for(size_t i = 0; i < vs.size(); i++) {
      if(!isthrough[vs[i]]) {
        S.push_back(vector<int>());
        SccDfsTwo(S.back(), vs[i], k++);
      }
    }

    H.resize(k);
    fill(isthrough.begin(), isthrough.end(), false);
    for(size_t i = 0; i < k; i++) {
      for(size_t j = 0; j < S[i].size(); j++) {
        int v = S[i][j];
        for(size_t x = 0; x < G[v].size(); x++) {
          int u = G[v][x];
          if(isthrough[cmp[u]] or cmp[v] == cmp[u]) continue;
          isthrough[cmp[u]] = true;
          H[cmp[v]].push_back(cmp[u]);
        }
      }
      for(size_t j=0; j<H[i].size(); j++) isthrough[H[i][j]] = false;
    }
    return make_pair(cmp, k);
  }
};

int main() {
  
  int n, m; cin >> n >> m;
  vector<vector<int>> g(n);
  for (int i = 0; i < m; i++) {
    int a, b; cin >> a >> b;
    g[a].push_back(b);
  }

  StronglyConnectedComponents scc(g);

  auto res = scc.scc();
  auto c = res.first;
  auto k = res.second;

  vector<vector<int>> ans(k);
  for (int i = 0; i < n; i++) {
    ans[c[i]].push_back(i);
  }

  cout << k << endl;
  for (int i = 0; i < k; i++) {
    cout << ans[i].size() << " ";
    for (int j = 0; j < ans[i].size(); j++) {
      cout << ans[i][j];
      if (j + 1 == ans[i].size()) cout << endl;
      else cout << " ";
    }
  }
  return 0;
}


Measured memory_bytes: 126804