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