N, M = map(int, input().split()) G = [[] for i in range(N)] deg = [0] * N for i in range(M): g, r = map(int, input().split()) g -= 1 H = list(map(int, input().split())) for h in H: G[h - 1].append(g) deg[g] += 1 stack = [] for i in range(N): if deg[i] == 0: stack.append(i) while stack: p = stack.pop() for u in G[p]: deg[u] -= 1 if deg[u] == 0: stack.append(u) print(deg.count(0))