# Your code here!

n = gets.chomp.to_i
ary = []
n.times do
  ary << gets.chomp.to_i
end
ary.sort!
median = ary[ary.length/2]
ans = []
n.times do |i|
  ans[i] = ary[i] - median
  ans[i] = ans[i].abs
end
ans.sort!
print(ans.sum*2-ans[0]-ans[1])
