main = interact $ unlines . map (show . osaki) . read' . lines
 where
  read' ("0":_) = []
  read' (n:xs) = map words hms : read' xs'
   where (hms,xs') = splitAt (read n) xs

osaki hms = osaki' [] hms
 where
  osaki' acc [] = length acc
  osaki' [] ([d,a]:xs) = osaki' [a] xs
  osaki' acc ([d,a]:xs)
   | d <= maximum acc = osaki' (a : tail acc) xs
   | otherwise = osaki' (a:acc) xs