{-# LANGUAGE BangPatterns #-}
import Control.Arrow
import Data.Int
import Data.Foldable
import qualified Data.Vector.Unboxed as VU
import qualified Data.ByteString.Char8 as B
import Debug.Trace

newtype FixF f = InF { outF :: f (FixF f) }

cata :: Functor f => (f a -> a) -> FixF f -> a
cata phi = phi . fmap (cata phi) . outF

ana :: Functor f => (a -> f a) -> a -> FixF f
ana psi = InF . fmap (ana psi) . psi

hylo :: Functor f => (f x -> x) -> (y -> f y) -> (y -> x)
hylo phi psi = cata phi . ana psi

data Node f a x = Node !a !(f x)
  deriving Show

instance Functor f => Functor (Node f a) where
  fmap f (Node a fx) = Node a (fmap f fx)

newtype Cofree f a = Cofree { runCofree :: FixF (Node f a) }

instance Functor f => Functor (Cofree f) where
  fmap f = Cofree . ana (phi . outF) . runCofree where
    phi (Node a b) = Node (f a) b

extract :: Functor f => Cofree f a -> a
extract cf = case outF (runCofree cf) of
  Node a _ -> a

sub :: Functor f => Cofree f a -> f (Cofree f a)
sub cf = case outF (runCofree cf) of
  Node _ b -> fmap Cofree b

histo :: Functor f => (f (Cofree f a) -> a) -> FixF f -> a
histo phi = extract . cata ap
  where ap a = Cofree $ InF $ Node (phi a) (fmap runCofree a)

dyna :: Functor f => (f (Cofree f x) -> x) -> (y -> f y) -> (y -> x)
dyna phi psi = extract . hylo ap psi
  where ap a = Cofree $ InF $ Node (phi a) (fmap runCofree a)

data DP a = N | P Int !a
  deriving (Eq, Show)
instance Functor DP where
  fmap f N = N
  fmap f (P n a) = P n (f a)

solve :: Int -> VU.Vector Int16 -> Int
solve n hs = fromIntegral $ dyna phi psi $ (n - 1)
 where
  psi 0 = N
  psi n = P n (n - 1)

  phi N        = 0
  phi (P n dp) = min f1 f2
   where
    h  = hs VU.! n
    f1 = extract dp + abs (h - hs VU.! (n - 1))
    f2 = case sub dp of
      N     -> abs $ hs VU.! 1 - hs VU.! 0
      P _ a -> extract a + abs (h - hs VU.! (n - 2))

main = do
  let readInt :: Integral a => B.ByteString -> Maybe (a, B.ByteString)
      readInt = fmap (first fromIntegral . second B.tail) . B.readInt

  n  <- (\vec -> (vec VU.! 0)) . VU.unfoldrN 1 readInt <$> B.getLine
  hs <- VU.unfoldrN n readInt <$> B.getLine
  print $ solve n hs
