#!/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 += stands

        if len({c for stand in stands for c in stand}) != K:
            print("-1")
            continue

        bought = K * [0]
        null = K

        start = end = 0

        best = float('inf')
        su = 0
        while True:
            if null > 0:
                if end == 2*N:
                    break
                for c in stands[end]:
                    if bought[c] == 0:
                        null -= 1
                    bought[c] += 1
                    su += 1
                end += 1

            elif null == 0:
                for c in stands[start]:
                    bought[c] -= 1
                    su -= 1
                    if bought[c] == 0:
                        null += 1
                start += 1

            if null == 0:
                if best > su:
                    best = su
        print(best)
except EOFError:
    pass
