main = do
  getLine
  h <- map read . words <$> getLine
  print . subtract 1 . length . takeWhile (/= []) $ iterate shower h

shower h = hnext ++ dropWhile (/= 0) htrim
  where
    htrim = dropWhile (== 0) h
    hnext = map (subtract 1) $ takeWhile (/= 0) htrim