module Trained where

import qualified Data.Array as A
import qualified Data.IntSet as S
import Data.Bool

main :: IO ()
main = do
  { n:as <- map read . words <$> getContents
  ; print $ solve $! A.listArray (1, n) as
  }

{- |
>>> solve $ A.listArray (1,3) [3,1,2]
2
-}
solve :: A.Array Int Int -> Int
solve bs = iter S.empty 1 1
  where
    iter s b c = bool (bool (iter (S.insert b s) b' (succ c)) c  (b' == 2)) (-1) (S.member b s)
      where
        b' = bs A.! b

readLineInputs :: Read a => IO [a]
readLineInputs = map read . words <$> getLine
