import Control.Monad

main :: IO ()
main = do
  n <- readLn :: IO Int
  bs <- replicateM n readLn :: IO [Int]
  let zs = zip [1..n] bs
  print $ access (head zs) zs [] 
  
access :: (Int, Int) -> [(Int, Int)] -> [Int] -> Int
access (_,v) zs acc = 
  let (ni, nv) = head $ filter (\z -> (fst z) == v) zs
     in if nv == 2 
           then 
             length acc + 2
           else
             if any (==nv) acc
                then 
                  -1
                else
                  access (ni, nv) zs (nv:acc)
                     