n, m=gets.split.map(&:to_i) g=Array.new(n){[]} deg=Array.new(n){0} m.times do gi, ri=gets.split.map(&:to_i) h=gets.split.map(&:to_i) deg[gi-1]+=ri h.each{|v| g[v-1].push(gi-1)} end q=[] n.times do |i| q.push(i) if deg[i]==0 end tot=0 while q.size>0 u=q.shift tot+=1 g[u].each do |v| next if deg[v]==0 q.push(v) if (deg[v]-=1)==0 end end puts tot