from statistics import median import math n,k=map(int,input().split()) a=list(map(int,input().split())) c=0 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 i==1: c=min(e,f) else: c=min(e,f,c) else: print(c)