
import Data.Maybe (isJust, fromJust)

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 = length . takeWhile isJust . iterate (update hs.fromJust) $ update hs [0,0..]

main = do
  getLine
  hs <- map read . words <$> getLine :: IO [Int]
  print $ solv hs 

