import Control.Monad
import Data.IntMap.Strict (IntMap, empty, insert, (!))
 
main = do
  n <- readLn
  print . g n n 1 . f 1 <$> replicateM n readLn
 
f _ [] = empty
f k (x:xs) = insert k x . f (k+1) $ xs
 
g n t x as
  | x == 2 = n - t
  | t < 0 = -1
  | otherwise = g n (t-1) (as ! x) as
 