n, k = map(int, input().split()) A = list(map(int, input().split())) med = n // 2 Am1 = A[med] Am2 = A[med - 1] A0 = [abs(a - Am2) for a in A] A0.sort() A1 = [abs(a - Am1) for a in A] A1.sort() A2 = [abs(a - A[0]) for a in A] A2.sort() A3 = [abs(a - A[-1]) for a in A] A3.sort() ans0 = sum(A0[:k]) - sum(A0[k:]) ans1 = sum(A1[:k]) - sum(A1[k:]) ans2 = sum(A2[:k]) - sum(A2[k:]) ans3 = sum(A3[:k]) - sum(A3[k:]) print(min(ans0, ans1, ans2, ans3))