
update [] _ = Nothing
update (h:hs) (x:xs)
  | h == x = (x:) <$> update hs xs
  | h > x  = Just $ grow (h:hs) (x:xs)
  where
    grow [] _ = []
    grow (h:hs) (x:xs)
      | h == x = x:xs
      | h > x = (x + 1) : grow hs xs

solv hs = go hs (repeat 0) 0 where
  go hs xs i =
    case update hs xs of
      Nothing -> i
      Just updated -> go hs updated (succ i)

main = do
  getLine
  hs <- map read . words <$> getLine
  print $ solv hs
