import Control.Monad
import qualified Data.ByteString.Char8 as BC
import Data.List
import Data.Maybe (fromJust)
import qualified Data.IntMap as IM

readInts :: IO [Int]
readInts = map (fst . fromJust . BC.readInt) . BC.lines <$> BC.getContents

solve :: Int -> Int -> Int -> IM.IntMap Int -> Int
solve count id n as
  | button == 2 = count
  | count > n = -1
  | otherwise = solve (count + 1) button n as
  where button = fromJust $ IM.lookup id as

main :: IO ()
main = do
  n <- readLn
  as <- readInts
  print . solve 1 1 n $ IM.fromList $ zip [1..] as