import Control.Monad
 
main = do
  n <- readLn
  as <- map read <$> replicateM n getLine
  print $ f as [] 1
 
f as bs m
  | elem m bs = - 1 - length bs
  | m == 2 = 0
  | otherwise = 1 + f as (m:bs) (as !! m)