{-
import Control.Monad
import Data.Vector

main :: IO ()
main = do
  n <- readLn :: IO Int
  xs <- replicateM n readLn :: IO [Int]

  print $ count visited
  -}

import Control.Monad
import Data.Array

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

main :: IO ()
main = do
    n <- readLn :: IO Int
    xs <- replicateM n readLn :: IO [Int]
    let bs = listArray (1, n) xs
    print $ solve 1 n bs 0
