{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE ScopedTypeVariables #-}
import qualified Data.ByteString.Lazy.Char8 as B
import Data.Maybe
import Data.List
-- import qualified Data.Vector.Unboxed as VU
-- import qualified Data.Vector.Unboxed.Mutable as VUM
-- import Control.Monad
-- import Control.Monad.ST
-- import Debug.Trace

solve :: Int -> [Int] -> Int
solve n as = 2 * s - abs(e2-e1)
  where
    (e1, e2) = findMiddle n (sort as)
    s = sum [abs(a - e1)| a <- as]

findMiddle n bs = sub (divMod n 2)
  where
    sub (k,0) = let x:y:_ = drop (k-1) bs
                in (x, y)
    sub (k,1) = let x:y:z:_ = drop (k-1) bs
                in if y-x < z-y then (y, x) else (y, z)

readBInt :: B.ByteString -> Int
readBInt = fst . fromJust . B.readInt

tmain :: B.ByteString -> Int
tmain cont =
  let remLines0 = map B.words (B.lines cont)
      [bs_n]:remLines1 = remLines0
      n = readBInt bs_n
      as = map (\[x] -> readBInt x) remLines1
  in solve n as

outAnswer :: Int -> IO ()
outAnswer = putStrLn . show

main :: IO ()
main = outAnswer . tmain =<< B.getContents
