def median(t):
    t[len(t)/2]

def dist(t,m):
    x = median(t)
    #x = sum(t)/len(t)
    s = 0
    i = len(t)-1
    while i>=0 and t[i]>x:
        s += 2*(t[i]-x)
        i-=m
    j = 0
    while j<len(t) and t[j]<x:
        s += 2*(x-t[j])
        j+=m
    return s


N, M = map(int, raw_input().split())

A = map(int, raw_input().split())

print(dist(A,M))