main = do
 getLine
 a <- map read . words <$> getLine
 print $ solve (head a:head a:a) 0 0
 
solve :: [Integer] -> Integer -> Integer -> Integer
solve [a,b] x y = abs(a-b)
solve [a,b,c] x y = min(abs(a-c)+x) (abs(b-c)+y)
solve (a:b:c:cs) x y = solve (b:c:cs) y (min (abs(a-c)+x) (abs(b-c)+y))