import qualified Data.ByteString.Char8 as BS
import Data.Maybe
import Data.Array.IArray

solve :: Array Int Int -> Int
solve a = f 1 []
  where
    f n l = case n of
                2 -> length l
                _ | n `elem` l -> -1
                  | otherwise -> f (a!n) (n:l)

main :: IO ()
main = do
    n <- readLn :: IO Int
    a <- map (fst . fromJust . BS.readInt) . BS.lines <$> BS.getContents
    print . solve $ listArray (1,n) a
