#include <iostream>
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector <long long> V;
int main()
{
int n, m;
scanf("%d %d", &n, &m);
unsigned long long ans = 0;
long long x;
for (int i=1; i<=n; i++)
{
scanf("%lld", &x);
V.push_back(x);
}
long long med = V[n/2];
int pos = 0;
while (pos < n/2)
{
ans += (abs(V[pos]-med) * 2);
pos += m;
}
pos = V.size()-1;
while (pos > n/2)
{
ans += (abs(V[pos]-med) * 2);
pos -= m;
}
printf("%lld\n", ans);
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0ZGlvPgojaW5jbHVkZSA8aW9zdHJlYW0+CiNpbmNsdWRlIDx2ZWN0b3I+CiNpbmNsdWRlIDxhbGdvcml0aG0+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdmVjdG9yIDxsb25nIGxvbmc+IFY7CgppbnQgbWFpbigpCnsKICAgIGludCBuLCBtOwogICAgc2NhbmYoIiVkICVkIiwgJm4sICZtKTsKICAgIHVuc2lnbmVkIGxvbmcgbG9uZyBhbnMgPSAwOwogICAgbG9uZyBsb25nIHg7CiAgICBmb3IgKGludCBpPTE7IGk8PW47IGkrKykKICAgIHsKICAgICAgICBzY2FuZigiJWxsZCIsICZ4KTsKICAgICAgICBWLnB1c2hfYmFjayh4KTsKICAgIH0KICAgIGxvbmcgbG9uZyBtZWQgPSBWW24vMl07CiAgICBpbnQgcG9zID0gMDsKICAgIHdoaWxlIChwb3MgPCBuLzIpCiAgICB7CiAgICAgICAgYW5zICs9IChhYnMoVltwb3NdLW1lZCkgKiAyKTsKICAgICAgICBwb3MgKz0gbTsKICAgIH0KICAgIHBvcyA9IFYuc2l6ZSgpLTE7CiAgICB3aGlsZSAocG9zID4gbi8yKQogICAgewogICAgICAgIGFucyArPSAoYWJzKFZbcG9zXS1tZWQpICogMik7CiAgICAgICAgcG9zIC09IG07CiAgICB9CiAgICBwcmludGYoIiVsbGRcbiIsIGFucyk7CiAgICByZXR1cm4gMDsKfQ==