from statistics import median import math n,k=map(int,input().split()) a=list(map(int,input().split())) c=[] for i in range(0,n-k+1): s=a[i:i+k] t=median(s) b=math.ceil(t) d=math.floor(t) e=0 f=0 for u in range(0,k): e+=abs(b-a[i+u]) f+=abs(d-a[i+u]) else: if e>f: c.append(f) else: c.append(e) else: print(min(c))