#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, m, i, j, k, l;
  long long sum;
  sum = 0;
  cin >> n >> m;
  long long int a[n + 10];
  for (i = 1; i <= n; i++) {
    cin >> a[i];
  }
  sort(a + 1, a + n + 1);
  k = (n + 1) / 2;
  for (i = 1; i < k; i += m) {
    sum += 2 * (a[k] - a[i]);
  }
  for (i = n; i > k; i -= m) {
    sum += -2 * (a[k] - a[i]);
  }
  if (n % 2 == 1)
    cout << sum;
  else {
    long long sum1 = sum;
    sum = 0;
    k++;
    for (i = 1; i < k; i += m) {
      sum += 2 * (a[k] - a[i]);
    }
    for (i = n; i > k; i -= m) {
      sum += -2 * (a[k] - a[i]);
    }
    cout << min(sum1, sum);
  }
  return 0;
}
