import Control.Monad

main = do
  n <- readLn
  a <- map read <$> replicateM n getLine
  putStrLn . show $ solve n 1 1 a

solve:: Int -> Int -> Int -> [Int] -> Int
solve n c k a
  | an == 2 = k
  | n == k = -1
  | otherwise = solve n an (k + 1) a
  where
    an = a !! (c - 1)