#include <bits/stdc++.h>
using namespace std;
template <class P, class Q>
inline void smin(P &a, Q b) {
  if (b < a) a = b;
}
template <class P, class Q>
inline void smax(P &a, Q b) {
  if (a < b) a = b;
}
const int maxn = 1000000 + 100;
int n, m;
int a[maxn];
vector<int> p, q;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  smin(m, n);
  for (int i = 0, _n = (int)(n); i < _n; i++) cin >> a[i];
  sort(a, a + n);
  for (int i = 0; i < n; i += m) p.push_back(a[i]);
  for (int i = n - 1; i >= 0; i -= m) q.push_back(a[i]);
  reverse((q).begin(), (q).end());
  long long lsum = 0, rsum = 0;
  for (int i = 0, _n = (int)((int((q).size()))); i < _n; i++) rsum += q[i];
  long long ans = 1e18;
  for (int i = 0, _n = (int)((int((p).size()))); i < _n; i++) {
    lsum += p[i];
    int pos;
    pos = p[i];
    smin(ans, (i + 1) * (long long)pos - lsum + rsum -
                  ((int((p).size())) - i) * (long long)pos);
    pos = q[i];
    smin(ans, (i + 1) * (long long)pos - lsum + rsum -
                  ((int((p).size())) - i) * (long long)pos);
    rsum -= q[i];
  }
  cout << ans * 2 << endl;
  { return 0; }
}
