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

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

solve :: Int -> Int -> Int -> [(Int, Int)] -> Int
solve count index n as
  | button == 2 = count
  | count > n = -1
  | otherwise = solve (count + 1) (button - 1) n as
  where (_, button) = as !! index

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