using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
using System.ComponentModel;
static class Program{
	const int mod=(int)1e9+7;
	static List<int>[] li;
	static void Main(){
		Sc sc=new Sc();
		var s=sc.Ia;
		int n=s[0],m=s[1];
		li=new List<int>[n];
		for(int i=0;i<n;i++){li[i]=new List<int>();}
		for(int i=0;i<m;i++){
			var e=sc.Ia;
			li[e[0]].Add(e[1]);
		}
		var scc=new Scc(li);
	}
}
public class Scc{
	private const int o=0;
	public Scc(List<int>[] li){
		int n=li.Length,p=1,k=0;
		var b=new bool[n];
		var b2=new bool[n];
		var b3=new bool[n];
		var low=new int[n];
		var ska=new int[n];
		var lid=new List<List<int>>();
		for(int i = o;i<n;i++) {
			if(!b[i]){Fud(i);}
		}
		void Fud(int a){
			b[a]=true;
			low[a]=p++;
			ska[k++]=a;
			for(int i=0;i<li[a].Count;i++){
				if(b2[li[a][i]]){continue;}
				if(!b[li[a][i]]){Fud(li[a][i]);}
				if(low[a]>low[li[a][i]]){
					low[a]=low[li[a][i]];
					b3[a]=true;
				}
			}
			if(!b3[a]){
				int v=-1;
				lid.Add(new List<int>());
				while(v!=a){
					v=ska[--k];
					lid[lid.Count-1].Add(v);
					b2[v]=true;
				}
			}
		}
		StringBuilder sb=new StringBuilder();
		sb.Append(lid.Count+"\n");
		for(int i = lid.Count-1;i>=0;i--) {
			sb.Append(lid[i].Count).Append(" ").Append(string.Join(" ",lid[i])+"\n");
		}
		Console.Write(sb);
	}
}
public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public object[] Oa{get{return Console.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),long.Parse);}
	public double[] Da2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),double.Parse);}}
	public double[] Da3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),double.Parse);}
	public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
	public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
	public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
	public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}

Measured memory_bytes: 140352