import Control.Monad
import Data.List

solveSubSub [] xs = 1
solveSubSub (as:ass) xs
    | xs!!x' == y' = solveSubSub ass xs
    | otherwise = 0
        where
            x' = (as!!0) - 1
            y' = as!!1

solveSub [] xs = []
solveSub (ass:asss) xs
    | fst ass == 1 = (solveSubSub (snd ass) xs) : (solveSub asss xs)
    | otherwise = 0 : solveSub asss xs

solve [] [] = []
solve (asss:assss) (xs:xss)
    | ys == xs = (sum xs) : (solve assss xss)
    | otherwise = 0 : (solve assss xss)
    where
        ys = solveSub asss xs

main = do
    n <- readLn :: IO Int
    asss <- replicateM n $ do
        a <- readLn :: IO Int
        as <- replicateM a (map read . words <$> getLine) :: IO [[Int]]
        return $ as 
    let xss = replicateM n [0,1]
    print $ maximum $ solve (map (\x -> zip x asss) xss) xss
