import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.HashSet; import java.util.StringTokenizer; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author cteam014 */ public class Icecream { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tokenizer; String[] input; public void solve() throws Exception { String line; while ((line = reader.readLine()) != null) { int minimum = -1; tokenizer = new StringTokenizer(line); String[] nums = line.split(" "); int N = Integer.parseInt(nums[0]); // int N = Integer.parseInt(nums[0]); // int K = Integer.parseInt(nums[1]); //tokenizer.nextToken() input = new String[N]; for (int i = 0; i < N; i++) { input[i] = reader.readLine(); } int[] trues = new int[N]; int[] numSaples = new int[N]; boolean[][] dpMatrix = new boolean[N][K]; long iters = 0; long end = ((long)N)*((long)N); int stand = 0; while (trues[N - 1] < K && iters < end) { iters++; if (trues[stand % N] >= K) { continue; } tokenizer = new StringTokenizer(input[stand % N]); int L = Integer.parseInt(tokenizer.nextToken()); HashSet samples = new HashSet<>(); for (int i = 0; i < L; i++) { int sample = Integer.parseInt(tokenizer.nextToken()); samples.add(sample - 1); } for (int i = stand; i <= stand + N; i++) { numSaples[i % N] += L; for (int sample : samples) { if (! dpMatrix[i % N][sample]) { dpMatrix[i % N][sample] = true; trues[i % N]++; } } if (trues[i % N] >= K) { if ((minimum > 0 && numSaples[i % N] < minimum) || minimum < 0) { minimum = numSaples[i % N]; } } } ++stand; } if (minimum < 0) { System.out.println(minimum); } else { System.out.println(minimum - 1); } } } public static void main(String[] args) throws Exception { new Icecream().solve(); //long a = 100000; //System.out.println(a*a); } }