module Main where

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

-- 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 as x = let
      hs = mapMaybe safeHead as
      pairs = sort $ findPairs [] $ sort $ map flp $ zip [1..n] hs
      in
        if null pairs
          then -1
          else go n (tailAll 1 pairs as) (x+1)

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

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

flp (a, b) = if a < b then (a, b) else (b, a)

findPairs :: [Int] -> [(Int, Int)] -> [Int]
findPairs acc [x] = acc
findPairs acc ((a,b):y:xs) = let x = (a,b) in 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