#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000000 + 7;
const double esp = 1e-13;
long long ans;
int a[1000000 + 10], n, m, x, le, ri;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie();
  cin >> n >> m;
  for (long long i = 1; i <= n; i++) cin >> a[i];
  sort(a + 1, a + n + 1);
  ans = 0;
  if (n % 2 == 0) {
    le = n / 2;
    ri = le + 1;
    x = a[(le + ri) / 2];
    for (int ii = 1; ii <= le; ii += m) ans += x - a[ii];
    for (int ii = n; ii >= ri; ii -= m) ans += a[ii] - x;
  } else {
    le = n / 2 + 1;
    x = a[le];
    for (int ii = 1; ii <= le; ii += m) ans += x - a[ii];
    for (int ii = n; ii >= le; ii -= m) ans += a[ii] - x;
  }
  cout << ans * 2;
}
