import Prelude
import Control.Monad
import Control.Applicative


takahashi ::[Int]-> [Int]-> Int -> Int
takahashi as his now |now==2 = length his
                     |elem now his = -1
                     |otherwise = as `seq` his `seq` now `seq` takahashi as (now:his) $as!!(now-1)

main :: IO ()
main = do
	n <- readLn
	l <- replicateM n readLn
        putStrLn $ show $ takahashi l [] 1

