class Disciplina:
    def __init__(self):
        self.cod = 0
        self.name = ''
        self.grau = 0
        self.v = 0
        self.ativ = 1
        self.rem = 0

MAX = 210

vdisc = [Disciplina() for _ in range(MAX)]
adisc = [[0] * MAX for _ in range(MAX)]
ndisc = 0

listadisc = [0] * MAX
sizelista = 0

semestre = [[Disciplina() for _ in range(15)] for _ in range(MAX)]
discsem = [0] * MAX
nsemestre = 0

def finddisc(strdisc):
    for i in range(ndisc):
        if strdisc == vdisc[i].name:
            return i
    return -1

def DFS_Visit(u):
    depend = False
    vdisc[u].v = 1

    for i in range(vdisc[u].grau):
        if vdisc[adisc[u][i]].ativ:
            depend = True

        if not vdisc[adisc[u][i]].v and vdisc[adisc[u][i]].ativ:
            DFS_Visit(adisc[u][i])

    if not depend:
        vdisc[u].rem = 1
        listadisc[sizelista] = u
        globals()['sizelista'] += 1

def DFS():
    thereway = False
    for i in range(ndisc):
        vdisc[i].v = 0
    for i in range(ndisc):
        if not vdisc[i].v and vdisc[i].ativ:
            thereway = True
            DFS_Visit(i)
    return thereway

def ordenar():
    for i in range(sizelista - 1):
        for j in range(i + 1, sizelista):
            if listadisc[i] > listadisc[j]:
                listadisc[i], listadisc[j] = listadisc[j], listadisc[i]

def ordenar1(n):
    for i in range(discsem[n] - 1):
        for j in range(i, discsem[n]):
            if semestre[n][i].name > semestre[n][j].name:
                semestre[n][i], semestre[n][j] = semestre[n][j], semestre[n][i]

def main():
    global ndisc, sizelista, nsemestre

    while True:
        n, m = map(int, input().split())
        if n == 0 and m == 0:
            break

        ndisc = 0
        for i in range(MAX):
            vdisc[i].cod = i
            vdisc[i].name = ''
            vdisc[i].grau = 0
            vdisc[i].ativ = 1
            vdisc[i].rem = 0

        for i in range(n):
            strdisc = input().split()
            posdisc = finddisc(strdisc)
            if posdisc == -1:
                vdisc[ndisc].name = strdisc
                posdisc = ndisc
                ndisc += 1
            k = int(input())
            for j in range(k):
                strdep, = input().split()
                posdep = finddisc(strdep)
                if posdep == -1:
                    vdisc[ndisc].name = strdep
                    posdep = ndisc
                    ndisc += 1
                adisc[posdisc][vdisc[posdisc].grau] = posdep
                vdisc[posdisc].grau += 1

        nsemestre = 0
        while True:
            sizelista = 0
            if not DFS():
                break
            ordenar()

            discsem[nsemestre] = min(m, sizelista)

            for i in range(m):
                if i < sizelista:
                    vdisc[listadisc[i]].ativ = 0
                    semestre[nsemestre][i] = vdisc[listadisc[i]]

            ordenar1(nsemestre)

            nsemestre += 1

        print(f"Formatura em {nsemestre} semestres")
        for i in range(nsemestre):
            print(f"Semestre {i + 1} :", end="")
            for j in range(discsem[i]):
                print(f" {semestre[i][j].name}", end="")
            print()

if __name__ == "__main__":
    main()