import qualified Data.ByteString.Char8 as BC
import Data.Maybe

readIntsLine :: IO [Int]
readIntsLine = map (fst . fromJust . BC.readInt) . BC.words <$> BC.getLine

calc :: (Int, Int) -> (Int, Int) -> Int -> Int
calc (pc2, pc1) (ph2, ph1) h = min c2 c1 
  where c2 = pc2 + abs (ph2 - h)
        c1 = pc1 + abs (ph1 - h)

solve :: [Int] -> (Int, Int) -> (Int, Int) -> [Int]
solve [] _ _ = []
solve (h:hs) pcs@(pc2, pc1) phs@(ph2, ph1) = cost : next
  where cost = calc pcs phs h
        next = solve hs (pc1, cost) (ph1, h)

main :: IO ()
main = do
  getLine
  (h:hs) <- readIntsLine
  print . last $ solve hs (0, 0) (h, h)
