import Control.Monad                                                                                                       
                                                                                                                           
main :: IO ()                                                                                                              
main = do                                                                                                                  
  n <- read <$> getLine                                                                                                    
  as <- replicateM n (read <$> getLine)                                                                                    
                                                                                                                           
  print $ f n as 0 1                                                                                                       
                                                                                                                           
f :: Int -> [Int] -> Int -> Int -> Int                                                                                     
f n xs m i                                                                                                                 
  | i == 2 = m                                                                                                             
  | n == m = -1                                                                                                            
  | otherwise = f n xs (m+1) (xs!!(i-1))