B = int(input()) N = int(input()) l = [] for i in range(N): l.append(int(input())) n_median = 0 n_median1 = 0 if N % 2 == 0: n_median = N // 2 n_median1 = n_median - 1 else: n_median = (N -1) // 2 l.sort() ck = l[n_median] ck1 = l[n_median1] ans = 0 ans1 = 0 for l_i in l: ans += abs(l_i - ck) ans1 += abs(l_i - ck1) print(min(ans,ans1))