import Data.Array
main = do
  n <- readLn
  a <- listArray (1,n) . (map read . lines) <$> getContents
  print $ solve 1 a 0

solve :: Int -> Array Int Int -> Int -> Int
solve i a n
  | i == 2 = n
  | n > 100000 = -1
  | otherwise = solve (a ! i) a (n+1)