import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Text.Read as T
import qualified Data.Array as A

main :: IO ()
main = do
  (n : _) <- map unsafeSignedDecimal . T.words <$> T.getLine :: IO [Int]
  xyss <- (sequence . replicate n $ (unsafeSignedDecimal <$> T.getLine) >>= \a -> (sequence . replicate a $ fmap (== 1) . unsafeListToTuple . map unsafeSignedDecimal . T.words <$> T.getLine)) :: IO [[(Int, Bool)]]
  let
    isConsistent :: [Bool] -> Bool
    isConsistent bs = isConsistent' bs' xyss'
      where
        bs' = A.listArray (1, n) bs
        xyss' = map snd . filter fst $ zip bs xyss
  print . maximum . map (length . filter id) . filter isConsistent . sequence . replicate n $ [True, False]

isConsistent' :: A.Array Int Bool -> [[(Int, Bool)]] -> Bool
isConsistent' bs' = all (all p)
  where
    p (x, y) = bs' A.! x == y
{-# INLINE isConsistent' #-}

unsafeListToTuple :: [Int] -> (Int, Int)
unsafeListToTuple (x : y : _) = (x, y)

unsafeSignedDecimal :: T.Text -> Int
unsafeSignedDecimal s = case T.signed T.decimal s of
  Right (n, _) -> n
