class StronglyConnectedComponents
    def initialize(n)
        @normal=Array.new(n){[]}
        @reverse=Array.new(n){[]}
		@compo=Array.new(n){nil}
		@order=[]
		@used=Array.new(n){false}
    end
	def add(a, b)
		@normal[a] << b
		@reverse[b] << a
	end
	def dfs(i)
		return if @used[i]
		@used[i] = true
		@normal[i].each{|n|
			dfs(n)
		}
		@order<< i
	end
	def rdfs(i, cnt)
		return if @compo[i]
		@compo[i] = cnt
		@reverse[i].each{|n|
			rdfs(n, cnt)
		}
	end
	def build
		@normal.size.times{|i|
			dfs(i)
		}
		@order.reverse!
		group = 0
		@order.each{|o|
			next if @compo[o]
			rdfs(o, group)
			group+=1
		}
		warn @compo.inspect
		r = Array.new(group){[]}
		@compo.size.times{|i|
			r[@compo[i]] << i
		}
		r
	end
end 

N,M=gets.split.map(&:to_i)
scc=StronglyConnectedComponents.new(N)
M.times{
	a,b=gets.split.map(&:to_i)
	scc.add(a,b)
}

r=scc.build
puts r.size
r.each{|x|
	puts [x.size,*x].join(' ')
}
