import Control.Monad
import Data.List

solve :: Int -> [Int] -> (Int, Int)
solve n as =
  (\(x, y, _) -> (x, y)) $
    foldl'
      ( \(cnt, state, a) i ->
          if not (null a) && state /= 2 && state == i
            then (cnt + 1, head a, tail a)
            else (cnt, state, tail a)
      )
      (0, 1, as)
      [1 .. n]

main = do
  n <- readLn
  as <- replicateM n readLn
  let ans = solve n as
  print $ if snd ans == 2 then fst ans else -1
