{-# LANGUAGE OverloadedStrings #-}
module Main where

import           Data.List
import           Control.Monad
import           Debug.Trace
import qualified Data.ByteString.Char8         as BC
import           Data.Maybe                     ( fromJust )
import qualified Data.Set                      as S
import qualified Data.IntMap.Strict            as IM
import           Data.Char


readTuple2 :: IO (Int, Int)
readTuple2 = do
    [a, b] <- map (fst . fromJust . BC.readInt) . BC.words <$> BC.getLine
    return (a, b)

readTuple3 :: IO (Int, Int, Int)
readTuple3 = do
    [a, b, c] <- map (fst . fromJust . BC.readInt) . BC.words <$> BC.getLine
    return (a, b, c)

readInt :: IO Int
readInt = fst . fromJust . BC.readInt <$> BC.getLine

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

split :: (a -> Bool) -> [a] -> [[a]]
split f [] = []
split f as =
    let (fo, la) = span f as
        las      = case la of
            [] -> []
            xs -> split f $ tail xs
    in  case fo of
            [] -> las
            xs -> xs : las

distance :: Int -> Int -> Int -> Int
distance k from to =
        if from > to then to + k - from
        else to - from

solve :: Int -> [Int] -> Int
solve n hs = let
    heightList = IM.fromList $ zip [1..] hs
    ansList = IM.fromList [(1, 0), (2, abs (heightList IM.! 2 - heightList IM.! 1))]
    getPoint to from m = m IM.! from + abs (heightList IM.! from - heightList IM.! to)
    getAns h m = min (getPoint h (h - 2) m) (getPoint h (h - 1) m)
    ansList' = foldl' (\b a -> IM.insert a (getAns a b) b) ansList [3..n]
    in
    ansList' IM.! n
    

main = do
    n <- readInt
    hs <- readInts
    print $ solve n hs



