import sys
from queue import LifoQueue

def dfs1(v, graph, visited, postorder):
    stack = LifoQueue()
    stack.put([v,False])
    while not stack.empty():
        v,post = stack.get()
        if post:
            postorder.append(v)
        elif v not in visited:
            visited.add(v)
            stack.put([v, True])
            if v in graph:    
                for neighbour in graph[v]:
                    if neighbour not in visited:
                        stack.put([neighbour, False])

def dfs2(v, graph, visited, components):
    stack = LifoQueue()
    stack.put(v)
    while not stack.empty():
        v = stack.get()
        visited.add(v)
        components[v] = [current_component]
        if v in graph:
            for neighbour in graph[v]:
                if neighbour not in visited:
                    stack.put(neighbour)

def reversed_graph(graph):
    p = {}
    for v in graph:
        p[v] = []
    for v, neighbours in graph.items():
        for to in neighbours:
            p[to].append(v)
    return p

def main():
    global current_component

    graph = {}

    n,m = [int(x) for x in input().split()]
    for i in range(0,n):
        graph[i] = []

    for i in range(0,m):
        a,b = [int(x) for x in input().split()]
        graph.setdefault(a, [])
        graph[a].append(b)


    visited = set()
    postorder = []
    r_graph = reversed_graph(graph)
    for v in r_graph:
        if v not in visited:
            dfs1(v, r_graph, visited, postorder)

    visited = set()
    components = {}
    current_component = 0
    for v in reversed(postorder):
        if v not in visited:
            dfs2(v, graph, visited, components)
            current_component += 1

    visited = set()
    print(current_component)
    rc = reversed_graph(components)
    for i in sorted([i for i in components], reverse=True):
        if i not in visited:
            print(*sorted(rc[components[i][0]], reverse=True))
            visited.update(rc[components[i][0]])

if __name__ == '__main__': 
    main()