#include <bits/stdc++.h>
using namespace std;
vector<long long> fl;
int n, m;
long long f(int x) {
  int p = 0;
  long long ans = 0;
  while (p < x) {
    ans -= fl[p] - fl[x];
    p += m;
  }
  p = n - 1;
  while (p > x) {
    ans += fl[p] - fl[x];
    p -= m;
  }
  return ans;
}
void solution() {
  cin >> n >> m;
  for (int i = (1); i <= (n); i++) {
    int x;
    scanf("%d", &x);
    fl.push_back(x);
  }
  sort(fl.begin(), fl.end());
  int left = 0, right = n - 1;
  while (right > left + 2) {
    int m1 = left + (right - left) / 3;
    int m2 = right - (right - left) / 3;
    long long fm1 = f(m1), fm2 = f(m2);
    if (fm1 > fm2)
      left = m1;
    else
      right = m2;
  }
  cout << 2 * (((f(right)) < ((((f(left)) < (f((right + left) / 2)))
                                   ? (f(left))
                                   : (f((right + left) / 2)))))
                   ? (f(right))
                   : ((((f(left)) < (f((right + left) / 2)))
                           ? (f(left))
                           : (f((right + left) / 2)))))
       << '\n';
}
int main() { solution(); }
