import Data.Maybe
import Data.List 

main = do
 n <- getLine
 aa <- map (read::String-> Int) .lines <$> getContents
 let player = zip ([1..]::[Int]) aa ::[(Int,Int)]
 print .fromMaybe (-1) .play2 (play 1 player) $player
 
 
 
play  ::  Int -> [(Int,Int)] -> Int
play a x = snd.head.dropWhile (\i -> a/=fst i) $x


play2 :: Int -> [(Int,Int)] -> Maybe Int
play2 1 _ = Nothing
play2 2 _ = Just 1
play2 a x = Just(1+)  <*> play2 (play a x) x