import Control.Monad
import Data.Array

checkMan :: [(Int, Int)] -> Array Int Int -> Bool
checkMan [] _ = True
checkMan (p:ps) ar = check p ar && checkMan ps ar
  where check (idx, c) ar  = ar ! idx == c

patterns :: Int -> [Array Int Int]
patterns n = do
  xs <- replicateM n [0, 1]
  return $ listArray (1, n) xs

solve :: Int -> Array Int [(Int, Int)] -> [Array Int Int] -> Int
solve n ts [] = 0
solve n ts (x:xs) = if and cs then max (sum x) (solve n ts xs) else solve n ts xs
  where cs = do
          i <- [1..n]
          return (if x ! i == 1 then checkMan (ts ! i) x else True)

getTm :: IO [(Int, Int)]
getTm = do 
  a <- readLn
  replicateM a $ do 
    [i, t] <- (map read) . words <$> getLine :: IO [Int]
    return (i, t)

main = do
  n <- readLn
  xs <- replicateM n getTm 
  print $ solve n (listArray (1, n) xs) (patterns n)
