import Data.List
import Data.Maybe

main :: IO()
main = getContents >>= putStr.form.f.(++[1000]).map read.words.last.lines

form :: [Int] -> String
form xs = unlines [(unwords.map show.init) xs, show $last xs -1000]

f :: [Int] -> [Int]
f xs = let n = minimum xs in g n xs
    where g _ [] = []
          g m ys@(z:zs) | m == z = (z:) $f zs
                        | otherwise = let s = fromJust $elemIndex m ys in f  $((++ [last ys +1]).(m:).tail.init) $take s ys ++ [z] ++ drop (s+1) ys