M= int(input())
k1 = [i for i in map(int, input().split())]
# M=15
# k1=[1,1,1,1,1,1,1,2,2,2,2,3,3,3,3]
# M=5
# k1=[5, 10, 25, 25, 40]
# M=8
# k1=[2,3,5,7,11,13,17,19]
result=0
if M>2:
k1.sort()
k2=[k1[-1]]
del(k1[-1])
lrbiao=1
while k1:
if len(k1)==1:
if lrbiao==1:
k2.insert(0,k1[0])
break
else:
k2.append(k1[0])
break
if lrbiao==1:
k2.insert(0,k1[0])
del(k1[0])
k2.append(k1[0])
del(k1[0])
lrbiao=0
else:
k2.insert(0,k1[-1])
del(k1[-1])
k2.append(k1[-1])
del(k1[-1])
lrbiao=1
#最后两位相同的极端情况
if abs(k2[-1]-k2[-2])
temp=k2[-1]
k2.pop()
k2.insert(0,temp)
for x in range(M-1):
result+=abs((k2[x+1]-k2[x]))
elif M==2:
result=abs(k1[0]-k1[1])
print(result)