{-# LANGUAGE BangPatterns #-}

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

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

main :: IO ()
main = do
  [n] <- readInts
  bs <- readInts
  mapM_ print $ reverse $ solve2 n bs $ solve ([1..], bs) ([],[])
  -- print $ solve ([1..], bs) ([],[])

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

insert' :: Int -> [Int] -> [Int]
insert' j xs = as ++ [j] ++ bs
  where
    (as, bs) = splitAt (j-1) xs

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

-- 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)