import Control.Monad
import qualified Data.IntMap.Strict as IntMap
 
main = do
  n <- readLn
  as <- map read <$> replicateM n getLine
  print $ f n n 1 (mkd 1 as)
 
mkd _ [] = IntMap.empty
mkd k (a:as) = IntMap.insert k a (mkd (k+1) as)
 
f n t x d
  | t == -1 = -1
  | x == 2 = n - t
  | otherwise = f n (t-1) (d IntMap.! x) d