import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class Icecream {


    public static BufferedReader br;


    public static void main(String[] args) throws Exception {
        br = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while ((line = br.readLine()) != null) {
            solve(line, br);


        }
    }

    public static void solve(String line, BufferedReader br) throws Exception {
        List<Integer> result = new ArrayList<>();
        String[] firstLine = line.split(" ");
        int numberOfStands = Integer.parseInt(firstLine[0]);
        int species = Integer.parseInt(firstLine[1]);
        List<Stand> stands = new ArrayList<>();
        for (int i = 0; i < numberOfStands; i++) {
            line = br.readLine();
            String[] splittedLine = line.split(" ");
            Stand s = new Stand();

            for (int j = 1; j < splittedLine.length; j++) {
                s.species.add(Integer.parseInt(splittedLine[j]));
            }
            Set<Integer> uniqueSpecies = new HashSet<>(s.species);
            s.uniqueness = (double) uniqueSpecies.size() / (double) s.species.size();
            stands.add(s);
        }
        Comparator<Stand> comparator = new StandComparator();
        stands.sort(comparator);
        result.addAll(stands.get(0).species);
        stands.remove(0);
        for (int i = 0; i < numberOfStands; i++) {
            if(stands.size() == 0) {
                if (new HashSet<Integer>(result).size() == species) {
                    System.out.println(result.size());
                    return;
                }
                else {
                    System.out.println(-1);
                    return;
                }

            }
            recountUniqueness(stands, result);
            stands.sort(comparator);
            result.addAll(stands.get(0).species);
            stands.remove(0);
            if (new HashSet<Integer>(result).size() == species) {
                System.out.println(result.size());
                return;

            }
        }

        System.out.println(-1);
        return;
    }

    public static void recountUniqueness(List<Stand> stands, List<Integer> result) {
        for (Stand s : stands) {

            Set<Integer> temp = new HashSet<Integer>(s.species);
            temp.removeAll(new HashSet<Integer>(result));
            s.uniqueness = (double)(temp.size())/(double) s.species.size();
        }

    }
}
class Stand implements Comparable<Stand>{
    public List<Integer> species;
    public Double uniqueness;

    public Stand() {
        species = new ArrayList<>();
    }

    @Override
    public int compareTo(Stand stand) {
        return uniqueness.compareTo(stand.uniqueness);
    }
}

class StandComparator implements Comparator<Stand> {
    @Override
    public int compare(Stand stand, Stand t1) {
        int res = stand.compareTo(t1);
        if ( res== 0) {
            return -1* (stand.species.size() - t1.species.size());
        }
        return -1 * res;

    }


}
