#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template<class T>
void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }
#define repr(i,from,to) for (ll i=(ll)from; i<(ll)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i,N) repr(i,0,N)
#define per(i,N) for (int i=(int)N-1; i>=0; i--)

const ll MOD = pow(10,9)+7;
const ll LLINF = pow(2,61)-1;
const int INF = pow(2,30)-1;

vector<ll> fac;
void c_fac(int x=pow(10,6)+10) { fac.resize(x,true); rep(i,x) fac[i] = i ? (fac[i-1]*i)%MOD : 1; }
ll inv(ll a, ll m=MOD) { ll b = m, x = 1, y = 0; while (b!=0) { int d = a/b; a -= b*d; swap(a,b); x -= y*d; swap(x,y); } return (x+m)%m; }
ll nck(ll n, ll k) { return fac[n]*inv(fac[k]*fac[n-k]%MOD)%MOD; }
ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b) { return a/gcd(a,b)*b; }

// ----------------------------------------------------------------------
// ----------------------------------------------------------------------

struct StrongConnectedComponent {
  ll size, size_scc;
  vector<vector<ll>> adj, radj, adj_scc, vertex_set;
  vector<ll> vertex, vertex_scc;
  StrongConnectedComponent(ll N) {
    size = N; 
    adj.resize(N); radj.resize(N);
    vertex.resize(N); rep(i,N) vertex[i] = i;
  }
  void add_edge(ll from, ll to) { 
    adj[from].push_back(to);
    radj[to].push_back(from);
  }
  void dfs_std(ll n, ll &o, vector<ll> &order) {
    if (order[n]!=-1) return;
    order[n] = LLINF;
    for (auto x: adj[n]) if (order[x]==-1) dfs_std(x,o,order);
    order[n] = o; o--;
  }
  void dfs_rev(ll n, ll v, vector<ll> &vid) {
    vid[n] = v;
    for (auto x: radj[n]) if (vid[x]==-1) dfs_rev(x,v,vid);
  }
  void restruct(ll v, vector<ll> &vid) {
    size_scc = v;
    adj_scc.resize(v); vertex_scc.resize(v,0); vertex_set.resize(v);
    rep(i,size) {
      for (auto x: adj[i]) if (vid[i]!=vid[x]) adj_scc[vid[i]].push_back(vid[x]);
      vertex_scc[vid[i]] += vertex[i];
      vertex_set[vid[i]].push_back(i);
    }
  }
  void calc() {
    vector<ll> order(size,-1), revorder(size), vid(size,-1);
    ll o = size-1, v = 0;
    rep(i,size) dfs_std(i,o,order);
    rep(i,size) revorder[order[i]] = i;
    rep(i,size) if (vid[revorder[i]]==-1) { dfs_rev(revorder[i],v,vid); v++; }
    restruct(v,vid);
  }
  vector<ll> topo_sort() {
    vector<ll> rnum(size_scc,0), result;
    rep(i,size_scc) for (auto x: adj_scc[i]) rnum[x]++;
    queue<ll> top; rep(i,size_scc) if (rnum[i]==0) top.push(i);
    while (top.size()) {
      ll now = top.front(); top.pop();
      result.push_back(now);
      for (auto x: adj_scc[now]) { 
        rnum[x]--; 
        if (rnum[x]==0) top.push(x); 
      }
    }
    return result;
  }
  void output() {
    cout << size_scc << endl;
    vector<ll> topo = topo_sort();
    for (auto x: topo) {
      cout << vertex_set[x].size() << " ";
      debug(all(vertex_set[x]));
    }
  }
};

// ----------------------------------------------------------------------
// ----------------------------------------------------------------------

int main() {
  ll N, M; cin >> N >> M;

  StrongConnectedComponent scc(N);
  rep(i,M) {
    ll a, b; cin >> a >> b;
    scc.add_edge(a,b);
  } 

  scc.calc();
  scc.output();
  // cout << result << endl;
  return 0;
}

Measured memory_bytes: 120996