

from collections import defaultdict

import sys



input = sys.stdin.buffer.readline



def scc(V, E):

    # g = defaultdict(list)

    g = [[] for _ in range(len(V))]

    # rg = defaultdict(list)

    rg = [[] for _ in range(len(V))]

    for u, v in E:

        g[u].append(v)

        rg[v].append(u)





    o = []

    done = set()

    for u in V:

        if u in done:

            continue



        s = [(u, True)]

        while s:

            u, f = s.pop()

            if f:

                if u in done:

                    continue

                done.add(u)

                s.append((u, False))

                for v in g[u]:

                    if v in done:

                        continue

                    s.append((v, True))

            else:

                o.append(u)



    done = set()

    ans = []

    while o:

        u = o.pop()

        if u in done:

            continue



        s = [u]

        vv = [u]

        done.add(u)

        while s:

            u = s.pop()

            for v in rg[u]:

                if v in done:

                    continue

                vv.append(v)

                s.append(v)

                done.add(v)

        ans.append(vv)





    return ans





def atcoder_practice2_g():

    N, M = list(map(int, input().split()))



    E = []

    for _ in range(M):

        a, b = list(map(int, input().split()))

        E.append((a, b))

    V = list(range(N))

    vv = scc(V, E)

    print((len(vv)))

    for r in vv:

        print((len(r), *r))





def test():

    g = {

        1: [2],

        5: [2],

        2: [4, 3],

        3: [4, 7, 6],

        4: [5, 8],

        6: [9],

        7: [8],

        8: [9],

        9: [7, 10],

        10: []

    }



    V = list(range(1, 11))

    E = []

    for u, e in list(g.items()):

        for v in e:

            E.append((u, v))



    vv = scc(V, E)

    for r in vv:

        print(r)





if __name__ == "__main__":

    # test()

    atcoder_practice2_g()



from collections import defaultdict

import sys



input = sys.stdin.buffer.readline



def scc(V, E):

    g = defaultdict(list)

    # g = [[] for _ in range(len(V))]

    rg = defaultdict(list)

    # rg = [[] for _ in range(len(V))]

    for u, v in E:

        g[u].append(v)

        rg[v].append(u)





    o = []

    done = set()

    for u in V:

        if u in done:

            continue



        s = [(u, True)]

        while s:

            u, f = s.pop()

            if f:

                if u in done:

                    continue

                done.add(u)

                s.append((u, False))

                for v in g[u]:

                    if v in done:

                        continue

                    s.append((v, True))

            else:

                o.append(u)



    done = set()

    ans = []

    while o:

        u = o.pop()

        if u in done:

            continue



        s = [u]

        vv = [u]

        done.add(u)

        while s:

            u = s.pop()

            for v in rg[u]:

                if v in done:

                    continue

                vv.append(v)

                s.append(v)

                done.add(v)

        ans.append(vv)





    return ans





def atcoder_practice2_g():

    N, M = list(map(int, input().split()))



    E = []

    for _ in range(M):

        a, b = list(map(int, input().split()))

        E.append((a, b))

    V = list(range(N))

    vv = scc(V, E)

    print((len(vv)))

    for r in vv:

        print((len(r), *r))





def test():

    g = {

        1: [2],

        5: [2],

        2: [4, 3],

        3: [4, 7, 6],

        4: [5, 8],

        6: [9],

        7: [8],

        8: [9],

        9: [7, 10],

        10: []

    }



    V = list(range(1, 11))

    E = []

    for u, e in list(g.items()):

        for v in e:

            E.append((u, v))



    vv = scc(V, E)

    for r in vv:

        print(r)





if __name__ == "__main__":

    # test()

    atcoder_practice2_g()

from collections import defaultdict
import sys

input = sys.stdin.buffer.readline


def scc(V, E):
    # g = defaultdict(list)
    g = [[] for _ in range(len(V))]
    # rg = defaultdict(list)
    rg = [[] for _ in range(len(V))]
    for u, v in E:
        g[u].append(v)
        rg[v].append(u)
    o = []
    done = set()
    for u in V:
        if u in done:
            continue
        s = [(u, True)]
        while s:
            u, f = s.pop()
            if f:
                if u in done:
                    continue
                done.add(u)
                s.append((u, False))
                for v in g[u]:
                    if v in done:
                        continue
                    s.append((v, True))
            else:
                o.append(u)
    done = set()
    ans = []
    while o:
        u = o.pop()
        if u in done:
            continue
        s = [u]
        vv = [u]
        done.add(u)
        while s:
            u = s.pop()
            for v in rg[u]:
                if v in done:
                    continue
                vv.append(v)
                s.append(v)
                done.add(v)
        ans.append(vv)
    return ans


def atcoder_practice2_g():
    N, M = list(map(int, input().split()))
    E = []
    for _ in range(M):
        a, b = list(map(int, input().split()))
        E.append((a, b))
    V = list(range(N))
    vv = scc(V, E)
    print((len(vv)))
    for r in vv:
        print((len(r), *r))


def test():
    g = {
        1: [2],
        5: [2],
        2: [4, 3],
        3: [4, 7, 6],
        4: [5, 8],
        6: [9],
        7: [8],
        8: [9],
        9: [7, 10],
        10: [],
    }
    V = list(range(1, 11))
    E = []
    for u, e in list(g.items()):
        for v in e:
            E.append((u, v))
    vv = scc(V, E)
    for r in vv:
        print(r)


if __name__ == "__main__":
    # test()
    atcoder_practice2_g()

from collections import defaultdict
import sys

input = sys.stdin.buffer.readline


def scc(V, E):
    g = defaultdict(list)
    # g = [[] for _ in range(len(V))]
    rg = defaultdict(list)
    # rg = [[] for _ in range(len(V))]
    for u, v in E:
        g[u].append(v)
        rg[v].append(u)
    o = []
    done = set()
    for u in V:
        if u in done:
            continue
        s = [(u, True)]
        while s:
            u, f = s.pop()
            if f:
                if u in done:
                    continue
                done.add(u)
                s.append((u, False))
                for v in g[u]:
                    if v in done:
                        continue
                    s.append((v, True))
            else:
                o.append(u)
    done = set()
    ans = []
    while o:
        u = o.pop()
        if u in done:
            continue
        s = [u]
        vv = [u]
        done.add(u)
        while s:
            u = s.pop()
            for v in rg[u]:
                if v in done:
                    continue
                vv.append(v)
                s.append(v)
                done.add(v)
        ans.append(vv)
    return ans


def atcoder_practice2_g():
    N, M = list(map(int, input().split()))
    E = []
    for _ in range(M):
        a, b = list(map(int, input().split()))
        E.append((a, b))
    V = list(range(N))
    vv = scc(V, E)
    print((len(vv)))
    for r in vv:
        print((len(r), *r))


def test():
    g = {
        1: [2],
        5: [2],
        2: [4, 3],
        3: [4, 7, 6],
        4: [5, 8],
        6: [9],
        7: [8],
        8: [9],
        9: [7, 10],
        10: [],
    }
    V = list(range(1, 11))
    E = []
    for u, e in list(g.items()):
        for v in e:
            E.append((u, v))
    vv = scc(V, E)
    for r in vv:
        print(r)


if __name__ == "__main__":
    # test()
    atcoder_practice2_g()

-    # g = defaultdict(list) -    g = [[] for _ in range(len(V))] -    # rg = defaultdict(list) -    rg = [[] for _ in range(len(V))] +    g = defaultdict(list) +    # g = [[] for _ in range(len(V))] +    rg = defaultdict(list) +    # rg = [[] for _ in range(len(V))]