import Control.Monad (replicateM)
import Data.List (findIndex)

main :: IO ()
main = do
  n <- readLn
  xss <- replicateM n (map read . words <$> getLine) :: IO [[Int]]
  print $ simulate 0 xss

simulate n xs | xs == xs' = -1
              | all null xs' = n+1
              | otherwise = simulate (n+1) xs'
  where xs' = step xs

head' [] = 0
head' (x:xs) = x

tail' [] = []
tail' (_:xs) = xs

step xs = zipWith ($) (matching (map head' xs)) (map tail' xs)

matching :: [Int] -> [[Int] -> [Int]]
matching xs = zipWith f [1..] (map (\i -> if i == 0 then 0 else xs !! (i-1)) xs)
  where
    f i i' | i == i' || i' == 0 = id
           | otherwise = let x' = xs !! (i-1) in (x':)
