N,K = map(int,input().split()) A = list(map(int,input().split())) if not K: tmp1 = -sum(abs(A[i]-A[0]) for i in range(N)) tmp2 = -sum(abs(A[i]-A[N-1]) for i in range(N)) exit(print(min(tmp1,tmp2))) imos = [A[i] for i in range(N)] for i in range(1,N): imos[i] += imos[i-1] res = 10**18 l = 0 for i in range(N-K+1): while l+K < N and A[l+K] - A[i] < A[i] - A[l]: l += 1 tmp = 0 tmp += imos[l-1] * (l > 0) - A[i] * l tmp += A[i] * (i - l + 1) - imos[i] + imos[l-1] * (l > 0) tmp += imos[l+K-1] - imos[i] - A[i] * (l + K - i - 1) tmp += A[i] * (N - l - K) - imos[N-1] + imos[l+K-1] res = min(res,tmp) print(res)