#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const double PI = acos(-1.0);
const int inf = (int)1e9 + 7;
const long long INF = (long long)1e18 + 7;
const int mod = (int)1e9 + 7;
const int N = (int)1e6 + 7;
int n, m, a[N], mark[N], cnt, cntr, cntl;
long long ans = INF * 8, res, suml, sumr;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%d", a + i);
  sort(a, a + n);
  for (int i = n - 1; i > 0; i--) {
    if (cnt == 0) sumr += a[i], mark[i] = 1, cntr++;
    cnt++;
    if (cnt == m) cnt = 0;
  }
  cnt = 0;
  for (int i = 0; i < n; i++) {
    if (mark[i]) sumr -= a[i], cntr--;
    ans = min(ans, ((cntl - cntr) * 1ll * a[i] - suml + sumr) * 2);
    if (cnt == 0) suml += a[i], cntl++;
    cnt++;
    if (cnt == m) cnt = 0;
  }
  printf("%I64d\n", ans);
  return 0;
}
