N, K, *A = map(int, open(0).read().split()) B = [1] * K + [-1] * (N - K) t = 0 x = A[0] for i in range(N): t += B[i] * abs(x - A[i]) B = [-1] * (N - K) + [1] * K u = 0 x = A[-1] for i in range(N): u += B[i] * abs(x - A[i]) print(min(t, u))