def SCC_Tarjan(g):

    def dfs(v):

        nonlocal idx

        used[v] = 1

        pre[v] = low[v] = idx

        idx += 1

        S.append(v)

        onS[v] = 1

        for c in g[v]:

            if not used[c]:

                dfs(c)

                low[v] = min(low[v], low[c])

            elif onS[c]:

                low[v] = min(low[v], pre[c])

        used[v] = 2



        if low[v] == pre[v]:

            cc = []

            while True:

                w = S.pop()

                onS[w] = 0

                cc.append(w)

                if w==v: break

            scc.append(cc)





    n = len(g)

    used = [0]*n # 0 -> 1 -> 2

    pre = [0]*n

    low = [0]*n

    onS = [0]*n

    idx = 0

    S = []

    scc = []

    for i in range(n):

        if not used[i]:

            dfs(i)

    #print(g)    

    #print("idx",pre)

    #print("low",low)

    #print(scc)

        

    return scc



###########################################################################                        

###########################################################################                        



# coding: utf-8

# Your code here!

import sys

readline = sys.stdin.readline

read = sys.stdin.read

sys.setrecursionlimit(10**6)

#n,*w = [int(i) for i in read().split()]



n,m = [int(i) for i in readline().split()]



g = [[] for _ in range(n)]

for _ in range(m):

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

    g[a].append(b)



gp = SCC_Tarjan(g)

print((len(gp)))

for lst in gp[::-1]:

    print((*([len(lst)]+lst)))



def SCC_Tarjan(g):

    def dfs(v):

        nonlocal idx

        pre[v] = low[v] = idx

        idx += 1

        S.append(v)

        #onS[v] = 1

        for c in g[v]:

            if pre[c] == -1: 

                dfs(c)

                low[v] = min(low[v], low[c])

            #elif onS[c]:

            else:

                low[v] = min(low[v], pre[c])



        if low[v] == pre[v]:

            cc = []

            while True:

                w = S.pop()

                #onS[w] = 0

                pre[w] = n

                cc.append(w)

                if w==v: break

            scc.append(cc)





    n = len(g)

    pre = [-1]*n # 負なら未処理、[0,n) ならpre-order, n ならvisited

    low = [0]*n

    onS = [0]*n

    idx = 0

    S = []

    scc = []

    for i in range(n):

        if pre[i] == -1:

            dfs(i)

    

    #print(g)    

    #print("idx",pre)

    #print("low",low)

    #print(scc)

        

    return scc



###########################################################################                        

###########################################################################                        



# coding: utf-8

# Your code here!

import sys

readline = sys.stdin.readline

read = sys.stdin.read

sys.setrecursionlimit(10**6)

#n,*w = [int(i) for i in read().split()]



n,m = [int(i) for i in readline().split()]



g = [[] for _ in range(n)]

for _ in range(m):

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

    g[a].append(b)



gp = SCC_Tarjan(g)

print((len(gp)))

for lst in gp[::-1]:

    print((*([len(lst)]+lst)))

def SCC_Tarjan(g):
    def dfs(v):
        nonlocal idx
        used[v] = 1
        pre[v] = low[v] = idx
        idx += 1
        S.append(v)
        onS[v] = 1
        for c in g[v]:
            if not used[c]:
                dfs(c)
                low[v] = min(low[v], low[c])
            elif onS[c]:
                low[v] = min(low[v], pre[c])
        used[v] = 2
        if low[v] == pre[v]:
            cc = []
            while True:
                w = S.pop()
                onS[w] = 0
                cc.append(w)
                if w == v:
                    break
            scc.append(cc)

    n = len(g)
    used = [0] * n  # 0 -> 1 -> 2
    pre = [0] * n
    low = [0] * n
    onS = [0] * n
    idx = 0
    S = []
    scc = []
    for i in range(n):
        if not used[i]:
            dfs(i)
    # print(g)
    # print("idx",pre)
    # print("low",low)
    # print(scc)
    return scc


###########################################################################
###########################################################################
# coding: utf-8
# Your code here!
import sys

readline = sys.stdin.readline
read = sys.stdin.read
sys.setrecursionlimit(10**6)
# n,*w = [int(i) for i in read().split()]
n, m = [int(i) for i in readline().split()]
g = [[] for _ in range(n)]
for _ in range(m):
    a, b = list(map(int, readline().split()))
    g[a].append(b)
gp = SCC_Tarjan(g)
print((len(gp)))
for lst in gp[::-1]:
    print((*([len(lst)] + lst)))

def SCC_Tarjan(g):
    def dfs(v):
        nonlocal idx
        pre[v] = low[v] = idx
        idx += 1
        S.append(v)
        # onS[v] = 1
        for c in g[v]:
            if pre[c] == -1:
                dfs(c)
                low[v] = min(low[v], low[c])
            # elif onS[c]:
            else:
                low[v] = min(low[v], pre[c])
        if low[v] == pre[v]:
            cc = []
            while True:
                w = S.pop()
                # onS[w] = 0
                pre[w] = n
                cc.append(w)
                if w == v:
                    break
            scc.append(cc)

    n = len(g)
    pre = [-1] * n  # 負なら未処理、[0,n) ならpre-order, n ならvisited
    low = [0] * n
    onS = [0] * n
    idx = 0
    S = []
    scc = []
    for i in range(n):
        if pre[i] == -1:
            dfs(i)
    # print(g)
    # print("idx",pre)
    # print("low",low)
    # print(scc)
    return scc


###########################################################################
###########################################################################
# coding: utf-8
# Your code here!
import sys

readline = sys.stdin.readline
read = sys.stdin.read
sys.setrecursionlimit(10**6)
# n,*w = [int(i) for i in read().split()]
n, m = [int(i) for i in readline().split()]
g = [[] for _ in range(n)]
for _ in range(m):
    a, b = list(map(int, readline().split()))
    g[a].append(b)
gp = SCC_Tarjan(g)
print((len(gp)))
for lst in gp[::-1]:
    print((*([len(lst)] + lst)))

-        used[v] = 1 -        onS[v] = 1 +        # onS[v] = 1 -            if not used[c]: +            if pre[c] == -1: -            elif onS[c]: +            # elif onS[c]: +            else: -        used[v] = 2 -                onS[w] = 0 +                # onS[w] = 0 +                pre[w] = n -    used = [0] * n  # 0 -> 1 -> 2 -    pre = [0] * n +    pre = [-1] * n  # 負なら未処理、[0,n) ならpre-order, n ならvisited -        if not used[i]: +        if pre[i] == -1: