n=gets.chomp.to_i
a=[]

n.times do |i|
  a[i] = gets.chomp.to_i
end

def abs(n)
  return -n if n<0
  return n
end

a = a.sort
bigger = a.select { |e| e > a[n/2]  }.reverse
smaller = a.select { |e| e <= a[n/2]  }.drop(1)

left = []
right = []

left[0] = a[0]
right[0] = a[0]
tmp = 1
indexb = 0
indexs = 0
index = 1


for i in 1..(n+1) do
  if tmp % 2 == 1
    left[index] = bigger[indexb] if bigger[indexb + 1] != nil
    right[index] = bigger[indexb+1] if bigger[indexb + 1] != nil
    right[index] = bigger[indexb] if bigger[indexb + 1] == nil && bigger[indexb] != nil
    index += 1 if bigger[index] != nil
    tmp += 1
    indexb += 2
  else
    right[index] = smaller[indexs] if smaller[indexs] != nil
    left[index] = smaller[indexs+1] if smaller[indexs+1] != nil
    index += 1
    tmp += 1
    indexs += 2
  end
end

tmp = left[0]
sum = 0
for i in left do
  sum += abs(tmp-i)
  tmp = i
end
tmp = right[0]
for i in right do
  sum += abs(tmp-i)
  tmp = i
end


puts sum
