solve :: [Int] -> Int
solve [h1, h2] = abs (h1 - h2)
solve [h1, h2, h3] = min root1 root2
  where root1 = abs (h1 - h2) + solve [h2, h3]
        root2 = abs (h1 - h3)
solve (h1:h2:h3:hx) = min root1 root2
  where root1 = abs (h1 - h2) + solve (h2:h3:hx)
        root2 = abs (h1 - h3) + solve (h3:hx)

main :: IO ()
main = do
  _ <- getLine
  heights <- map read . words <$> getLine
  print $ solve heights