package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	m := getNextInt(scanner)
	g := NewSccGraph(n)
	for i := 0; i < m; i++ {
		a := getNextInt(scanner)
		b := getNextInt(scanner)
		g.AddEdge(a, b)
	}
	group := g.Scc()
	fmt.Fprintln(writer, len(group))
	for i := 0; i < len(group); i++ {
		fmt.Fprintf(writer, "%d", len(group[i]))
		for j := 0; j < len(group[i]); j++ {
			fmt.Fprintf(writer, " %d", group[i][j])
		}
		fmt.Fprintln(writer, "")
	}
}

type Csr struct {
	Start []int
	Elist []*Edge
}
type Pair struct {
	From int
	E    *Edge
}
type Edge struct {
	To int
}
type SccGraph struct {
	n     int
	edges []*Pair
}

func NewCsr(n int, edges []*Pair) *Csr {
	start := make([]int, n+1)
	counter := make([]int, n+1)
	elist := make([]*Edge, len(edges))
	for _, e := range edges {
		start[e.From+1]++
	}
	for i := 0; i < n; i++ {
		start[i+1] += start[i]
	}
	copy(counter, start)
	for _, e := range edges {
		elist[counter[e.From]] = e.E
		counter[e.From]++
	}

	return &Csr{Start: start, Elist: elist}
}
func NewSccGraph(n int) *SccGraph {
	return &SccGraph{n: n, edges: make([]*Pair, 0)}
}
func (s *SccGraph) AddEdge(from, to int) {
	s.edges = append(s.edges, &Pair{From: from, E: &Edge{To: to}})
}
func (s *SccGraph) SccIds() (int, []int) {
	g := NewCsr(s.n, s.edges)
	var now, group int
	n := s.n
	visited := make([]int, 0)
	low := make([]int, n)
	ord := make([]int, n)
	ids := make([]int, n)
	for i := 0; i < n; i++ {
		ord[i] = -1
	}
	var dfs func(int)
	dfs = func(v int) {
		low[v] = now
		ord[v] = now
		now++
		visited = append(visited, v)
		for i := g.Start[v]; i < g.Start[v+1]; i++ {
			to := g.Elist[i].To
			if ord[to] == -1 {
				dfs(to)
				if low[v] > low[to] {
					low[v] = low[to]
				}
				continue
			}
			if low[v] > ord[to] {
				low[v] = ord[to]
			}
		}
		if low[v] == ord[v] {
			for i := len(visited) - 1; i >= 0; i-- {
				u := visited[i]
				ord[u] = n
				ids[u] = group
				visited = visited[:i]
				if v == u {
					break
				}
			}
			group++
		}
	}
	for i := 0; i < n; i++ {
		if ord[i] == -1 {
			dfs(i)
		}
	}
	for i := 0; i < n; i++ {
		ids[i] = group - 1 - ids[i]
	}
	return group, ids
}
func (s *SccGraph) Scc() [][]int {
	group, ids := s.SccIds()
	groups := make([][]int, group)
	for i := 0; i < s.n; i++ {
		groups[ids[i]] = append(groups[ids[i]], i)
	}
	return groups
}


Measured memory_bytes: 78996