import Data.List
toInt::String->Int
toInt = read
format::Show a=>[a]->String
format [x] = show x
format (x:xs) = show x ++ " " ++ format xs
format'::Show a=>(Int, [a])->String
format' (n, xs) = format xs ++ "\n" ++ show n
minBreak::Ord a=>[a]->([a], [a])
minBreak [x] = ([], [x])
minBreak (x:xs)
  | x > z = (x:ys, z:zs)
  | otherwise = ([], x:ys++z:zs)
    where
      (ys, (z:zs)) = minBreak xs
ssort::Ord a=>Int->[a]->[a]->(Int, [a])
ssort n xs [y] = (n, xs++[y])
ssort n xs (y:ys)
  | y > w = ssort (n+1) (xs++[w]) (zs++[y]++ws)
  | otherwise = ssort n (xs++[y]) (zs++[w]++ws)
    where
      (zs, (w:ws)) = minBreak ys
main = do
  getLine
  getLine >>=
    putStrLn.format'.(ssort 0 []).(map toInt).words
