n,m = map(int,input().split()) g = [[] for _ in range(n)] indegree = [0]*n for _ in range(m): x,_ = map(int,input().split()) x -= 1 for r in map(int,input().split()): g[r-1].append(x) indegree[x] += 1 ans = 0 q = [i for i in range(n) if indegree[i]==0] while q: v = q.pop() ans += 1 for c in g[v]: indegree[c] -= 1 if indegree[c] == 0: q.append(c) print(ans)