import sys sys.setrecursionlimit(10**7) def I(): return int(sys.stdin.readline().rstrip()) def MI(): return map(int,sys.stdin.readline().rstrip().split()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) def LI2(): return list(map(int,sys.stdin.readline().rstrip())) def S(): return sys.stdin.readline().rstrip() def LS(): return list(sys.stdin.readline().rstrip().split()) def LS2(): return list(sys.stdin.readline().rstrip()) N,K = MI() A = LI() ans0 = 0 for i in range(K): ans0 += abs(A[i]-A[0]) for i in range(K,N): ans0 -= abs(A[i]-A[0]) ans1 = 0 for i in range(N-1,N-K-1,-1): ans1 += abs(A[i]-A[0]) for i in range(N-K-1,-1,-1): ans1 -= abs(A[i]-A[0]) print(min(ans0,ans1))