import Data.Array
import qualified Data.IntSet as S

main = interact $ show . sol . map read . lines

sol (n:as) = f S.empty (a!1) 1
  where
  a = listArray (1,n) as
  f s i c
    | i `S.member` s  = -1
    | i == 2          = c
    | otherwise       = f (i `S.insert` s) (a!i) (c+1)