import Control.Monad

solve :: Int -> Int -> [Int] -> Int
solve d1 d2 [] = abs (d1 - d2)
solve d1 d2 [x, y, z] = dn
  where a  = d1 + abs (z - x)
        b  = d2 + abs (z - y)
        dn = min a b
solve d1 d2 (x:y:z:hs) = solve d2 dn (y:z:hs)
  where a  = d1 + abs (z - x)
        b  = d2 + abs (z - y)
        dn = min a b


main = do
  _ <- getLine
  h@(h0:h1:hs) <- map read . words <$> getLine
  print $ solve 0 (abs (h0 - h1)) h
