def main(): import sys from collections import defaultdict, deque n, m = map(int, sys.stdin.readline().split()) conditions = defaultdict(list) required = set() no_condition = set(range(1, n+1)) for _ in range(m): parts = sys.stdin.readline().split() g = int(parts[0]) r = int(parts[1]) h_list = list(map(int, sys.stdin.readline().split())) conditions[g] = h_list required.add(g) no_condition.discard(g) # Build the dependency graph graph = defaultdict(list) in_degree = defaultdict(int) all_nodes = set(range(1, n+1)) for g in conditions: for h in conditions[g]: graph[h].append(g) in_degree[g] += 1 # Compute the maximum subset that can be topologically sorted # Using Kahn's algorithm with possible nodes max_order = [] queue = deque() for node in all_nodes: if in_degree.get(node, 0) == 0: queue.append(node) while queue: node = queue.popleft() max_order.append(node) for neighbor in graph[node]: in_degree[neighbor] -= 1 if in_degree[neighbor] == 0: queue.append(neighbor) # The nodes not in the topological order cannot satisfy all their conditions # So we can't include them in the must-buy set must_buy = set(max_order) can_buy = no_condition - must_buy # Also, include any required nodes that are in the must_buy set must_buy = must_buy.intersection(required) res = len(must_buy) + len(no_condition) + len(can_buy) print(res) if __name__ == '__main__': main()