module Main where

import Control.Applicative
import Control.Monad
import Data.List
import qualified Data.Graph as G

dfs :: G.Forest Int -> G.Vertex -> Maybe Int
dfs gs v = iter 0 gs
  where
    iter _ []     = Nothing
    iter d ((G.Node r sf):ks)
        = if r == v then Just d else iter (d + 1) sf <|> iter d ks

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

    let g = G.buildG (1, n) (zip [1..] as)

    case dfs (G.dfs g [1]) 2 of
        Nothing -> print (-1)
        Just n  -> print n