import Data.List
main :: IO ()
main = do
  _ <- getLine
  ts <- parse . map (map read . words) . lines <$> getContents
  print $ solve ts

solve :: [[[Int]]] -> Int
solve = maximum . map (length.map snd) . filter validate. subsequences . addIndex
  where
    addIndex = zipWith (\n t -> (n, t)) [1..]

parse :: [[Int]] -> [[[Int]]]
parse [] = []
parse ls = h : parse rest
  where
    (h, rest) = break ((/=2).length) . tail $ ls

validate :: [(Int, [[Int]])] -> Bool
validate ts = honestsOk && unkindsOk
  where
    indexes = map fst ts
    honestsOk = and . map (`elem` indexes) . concatMap (map head . filter ((==1).head.tail) . snd) $ ts
    unkindsOk = and . map (`notElem` indexes) . concatMap (map head . filter ((==0).head.tail) . snd) $ ts