#include <bits/stdc++.h>
using namespace std;
long long pre[1000005], suf[1000005];
long long arr[1000005];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long n, m, i, j, x, y, mn = 1e18;
  cin >> n >> m;
  for (i = 0; i < n; i++) cin >> arr[i];
  sort(arr, arr + n);
  if (m >= n) {
    cout << (arr[n - 1] - arr[0]) * 2 << '\n';
    return 0;
  }
  pre[0] = arr[0];
  for (i = 1; i * m < n; i++) {
    pre[i] = pre[i - 1] + arr[i * m];
  }
  reverse(arr, arr + n);
  suf[0] = arr[0];
  for (i = 1; i * m < n; i++) {
    suf[i] = suf[i - 1] + arr[i * m];
  }
  reverse(arr, arr + n);
  for (i = 0; i < n; i++) {
    x = i / m + 1;
    y = (n - 1 - i) / m + 1;
    mn = min(mn, x * arr[i] - pre[x - 1] + suf[y - 1] - y * arr[i]);
  }
  cout << mn * 2 << '\n';
  return 0;
}
