from collections import defaultdict, deque def topological_sort(edges): #edges: directed edge ins = defaultdict(int) outs = defaultdict(list) for v1, v2 in edges: ins[v2] += 1 outs[v1].append(v2) q = deque([v for v in range(n) if ins[v] == 0]) res = [] while q: v1 = q.popleft() res.append(v1) for v2 in outs[v1]: ins[v2] -= 1 if ins[v2] == 0: q.append(v2) return res n, m = map(int, input().split()) edges = [] for _ in range(m): gi, ri = map(int, input().split()) h = list(map(int, input().split())) for hi in h: edges.append((hi-1, gi-1)) print(len(topological_sort(edges)))