#include <bits/stdc++.h>
using namespace std;
int a[1000006];
int n, m;
long long sol;
inline long long Solve(const int p) {
  long long sol = 0;
  int j = 0;
  for (int i = 1; i <= n && a[i] < p; i++) {
    ++j;
    if (j == m + 1) j = 1;
    if (j == 1) sol += 1LL * 2 * (p - a[i]);
  }
  j = 0;
  for (int i = n; i && a[i] > p; i--) {
    ++j;
    if (j == m + 1) j = 1;
    if (j == 1) sol += 1LL * 2 * (a[i] - p);
  }
  return sol;
}
int main() {
  cin.sync_with_stdio(false);
  cin >> n >> m;
  long long sum = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    sum += a[i];
  }
  sort(a + 1, a + n + 1);
  long long p = sum / n;
  sol = Solve(p);
  sol = min(sol, Solve(p - 1));
  sol = min(sol, Solve(p + 1));
  p = a[n / 2];
  sol = min(sol, Solve(p));
  p = a[n / 2 + 1];
  sol = min(sol, Solve(p));
  if (n / 2 - 1 > 0) {
    p = a[n / 2 - 1];
    sol = min(sol, Solve(p));
  }
  cout << sol << "\n";
  return 0;
}
