import Control.Applicative ((<$>)) import qualified Data.ByteString.Lazy.Char8 as LC import Data.Maybe (fromJust) minNumOfOps :: Integral a => a -> [a] -> a minNumOfOps n xs = mnoo n $ reverse xs where mnoo t [] = t mnoo t (y : ys) | y == t = flip mnoo ys $ t - 1 | otherwise = mnoo t ys main :: IO () main = do d <- map (fst . fromJust . LC.readInt) . LC.words <$> LC.getContents print $ minNumOfOps (head d) $ tail d