ans = 10 ** 18 n, k = map(int, input().split()) a = list(map(int, input().split())) b = [1] * k + [-1] * (n - k) x = a[0] ans = min(ans, sum(b[i] * abs(x - a[i]) for i in range(n))) x = a[-1] ans = min(ans, sum(b[i] * abs(x - a[i]) for i in range(n))) b = [-1] * (n - k) + [1] * k x = a[0] ans = min(ans, sum(b[i] * abs(x - a[i]) for i in range(n))) x = a[-1] ans = min(ans, sum(b[i] * abs(x - a[i]) for i in range(n))) print(ans)