module Main where

import           Control.Monad
import           Data.Maybe
import           Data.List

import           Debug.Trace

-- import           Data.Array
-- import           Data.Array.IArray

main :: IO ()
main = do
  n  <- readLn :: IO Int
  as <- replicateM n readN
  -- let arr = listArray (1, n) as :: Array Int [Int]
  print $ go n as 0
 where
  go n [] x = x
  go n ass x =
    {- traceShow (ass, x) $ -}
    let hs = map safeHead ass
        pairs =
          sort $ findPairs [] $ sort $ catMaybes $ zipWith flp [1 .. n] hs
    in  {- traceShow (pairs, ass) $ -}
        if null pairs
          then if all null ass then x else -1
          else go n (tailAll 1 pairs ass) (x + 1)

readN = map (read :: String -> Int) . words <$> getLine

safeHead []       = Nothing
safeHead (x : xs) = Just x

flp :: Int -> Maybe Int -> Maybe (Int, Int)
flp a Nothing  = Nothing
flp a (Just b) = if a < b then Just (a, b) else Just (b, a)

findPairs :: [Int] -> [(Int, Int)] -> [Int]
findPairs acc []  = acc
findPairs acc [x] = acc
findPairs acc (x@(a, b) : y : xs) =
  if x == y then findPairs (a : b : acc) xs else findPairs acc (y : xs)

tailAll _ []       ass        = ass
tailAll j (i : is) (as : ass) = if j == i
  then tail as : tailAll (j + 1) is ass
  else as : tailAll (j + 1) (i : is) ass
