def dist(Y,a,b): X=0 for y in Y: X+=min(abs(y-a),abs(y-b)) return X def solve(Y): if min(Y)==max(Y): return 1 Y.sort() p=N//3 q=(N//3)*2 A=float("inf") for a in range(max(p-5,0),min(p+5,N)): for b in range(max(q-5,0),min(q+5,N)): A=min(A,dist(Y,Y[a],Y[b])) return A N=int(input()) Y=list(map(int,input().split())) print(solve(Y))