// goodwincommaderek@gmail.com import java.util.*; public class islandmemories_dg_ac { public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); int M = in.nextInt(); // note that zones in this list do not contain the first node ArrayList> zones = new ArrayList<>(); for (int m = 0; m < M; m++) { int K = in.nextInt(); int[] zone = new int[K]; for (int k = 0; k < K; k++) { zone[k] = in.nextInt() - 1; } ArrayList zoneList = new ArrayList<>(); if (zone[0] == 0) { // add its complement to zones int ptr = 0; for (int n = 0; n < N; n++) { if (ptr < K && zone[ptr] == n) { // skip it ptr++; } else { // add it zoneList.add(n); } } } else { for (int i : zone) zoneList.add(i); } if (zoneList.size() > 0) { zones.add(zoneList); } } // sort the list in decreasing order of their sizes // note again that no zone in this list contains node 0 Collections.sort(zones, (a, b) -> { int comp1 = -Integer.compare(a.size(), b.size()); if (comp1 != 0) return comp1; return Integer.compare(a.get(0), b.get(0)); } ); // for each zone, let's give it a number. int[] prevZone = new int[N]; int uniqueZones = 1; // we start by making everything in a zone with the first node int ans = 1; // assume possible until proven otherwise for (int m = 0; m < zones.size(); m++) { // all of the zones of bigger size than it have been processed. // meaning, in order for the zone to be consistent, // prevZone[u] = prevZone[v] for all pairs of nodes (u,v) in a zone ArrayList zone = zones.get(m); // but first, is this a duplicate of the zone we checked last time? boolean duplicate = false; if (m > 0 && zones.get(m-1).size() == zone.size()) { duplicate = true; for (int i = 0; i < zone.size(); i++) { if (zone.get(i).intValue() != zones.get(m-1).get(i).intValue()) { duplicate = false; break; } } } if (duplicate) { continue; } else { uniqueZones++; } TreeSet uniqueZoneNums = new TreeSet<>(); for (Integer island : zone) { uniqueZoneNums.add(prevZone[island.intValue()]); } if (uniqueZoneNums.size() > 1) { ans = 0; break; } int zoneNum = m + 1; for (Integer island : zone) { prevZone[island.intValue()] = zoneNum; } } // now make sure all the unique zones are in prevZone TreeSet zoneSet = new TreeSet<>(); for(int zoneMarker : prevZone) zoneSet.add(zoneMarker); if (zoneSet.size() != uniqueZones) { ans = 0; } System.out.println(ans); } }