n = int(input()) l = sorted(map(int, input().split())) if len(set(l)) == 1: print(1) else: ans = [] for i in range(1, n): x = l[:i] y = l[i:] a = x[len(x)//2] b = 0 for i in x: b += abs(a-i) c = y[len(y)//2] d = 0 for i in y: d += abs(c-i) ans.append(b+d) print(min(ans))