import Data.List
import Data.Ord

main = interact $ unlines . map (show . infected_computer) . read' . lines
 where
  read' ("0 0":_) = []
  read' (nm:xs) = map ((\[t,s,d]->(t,(s,d))) . map read . words) ms : read' xs'
   where
    [_,m] = map read (words nm)
    (ms,xs') = splitAt m xs

infected_computer = length . foldl' infect [1] . map snd . sortBy (comparing fst)
 where infect acc (s,d) = if elem s acc && notElem d acc then d:acc else acc