import Prelude hiding (log)
import Control.Monad
-- import Control.Arrow ((&&&))
-- import Data.List (nub, sort, group)
-- import Data.Maybe (fromMaybe)
-- import qualified Data.IntMap as M

main :: IO ()
main = do
    n <- readLn
    as <- replicateM n readLn
    print $ solve (as, 0, []) 1

-- |
--
-- >>> solve ([3,1,2],0,[]) 1
-- 2
--
-- >>> solve ([3,4,1,2],0,[]) 1
-- -1
--
-- >>> solve ([3,3,4,2,4],0,[]) 1
-- 3
solve :: ([Int], Int, [Int]) -> Int -> Int
solve (as, cnt, log) x
  | y == 2       = cnt + 1
  | y `elem` log = -1
  | otherwise    = solve (as, cnt + 1, x : log) y
  where y = as !! (x - 1)
