#!/usr/bin/python

try:
    while True:
        N, K = map(int, input().split(' '))
        #stands = [list(map(lambda x: int(x) - 1, input().split(' ')))[1:] for _ in range(N)]
        stands = [list(map(lambda x: int(x) - 1, input().split(' '))) for _ in range(N)]
        stands += stands

        bought = K * [0]
        null = K

        start = end = 0

        best = None
        su = 0
        while True:
            if null > 0:
                if end == 2*N:
                    break
                for ci in range(1, len(stands[end])):
                    c = stands[end][ci]
#                for c in stands[end]:
                    if bought[c] == 0:
                        null -= 1
                    bought[c] += 1
                    su += 1
                end += 1

            elif null == 0:
                for ci in range(1, len(stands[start])):
                    c = stands[start][ci]
#                for c in stands[start]:
                    bought[c] -= 1
                    su -= 1
                    if bought[c] == 0:
                        null += 1
                start += 1

            if null == 0:
                if best is None or best > su:
                    best = su
        if best is None:
            print("-1")
        else:
            print(best)
except EOFError:
    pass
