{-# LANGUAGE BangPatterns #-}

import           Control.Applicative
import           Control.Monad
import           Control.Monad.Trans.State
import qualified Data.ByteString.Unsafe    as B
import qualified Data.ByteString.Char8     as C8
import           Data.Bits
import           Data.Char
import           Data.List
import           Data.Maybe
import           Data.Monoid
import           Data.Sequence (Seq)
import qualified Data.Sequence as Seq

type Input  = ([Int], [Int])
type Acc    = (IO (), Seq Int)
type Output = [(IO (), Seq Int)]

main :: IO ()
main = do
  [n] <- readInts
  bs <- readInts
  solve2 n (Seq.fromList bs) $ solve ([1..], bs) (return (), Seq.empty)

solve :: Input -> Acc -> Output
solve (_, []) acc = [acc]
solve (i:is, x:xs) (js, acc) = concatMap (solve bs) [(js >> print j, insertAt' j acc) | j <- [1..i]]
  where
    bs = (is, xs)

solve2 :: Int -> Seq Int -> Output -> IO ()
solve2 _ _ [] = print (-1)
solve2 n bs ((js,as):rest)
  | bs == as  = js
  | otherwise = solve2 n bs rest

insertAt' :: Int -> Seq Int -> Seq Int
insertAt' i s = (as Seq.|> i) Seq.>< bs
  where (as, bs) = Seq.splitAt (i-1) s

-- utils
type Pair = (Int, Int)

readInts :: IO [Int]
readInts = map (fst . fromJust . C8.readInt) . C8.words <$> C8.getLine

readPair :: IO Pair
readPair = head . unfoldr readT <$> C8.getLine
  where
    readT = runStateT $
      (,) <$> StateT (C8.readInt . C8.dropWhile isSpace)
          <*> StateT (C8.readInt . B.unsafeTail)