#include <bits/stdc++.h>
using namespace std;
int n, m;
long long int a[1000001], b[1000001], c[1000001];
long long f(int pos) {
  long long ret = 0, id1 = 0, id2 = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] < pos)
      b[++id1] = a[i];
    else if (a[i] > pos)
      c[++id2] = a[i];
  }
  for (int i = 1; i <= id1; i += m) ret += abs(pos - b[i]) * 2;
  for (int i = id2; i >= 1; i -= m) ret += abs(c[i] - pos) * 2;
  return ret;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
  long long lo = *min_element(a + 1, a + n + 1),
            hi = *max_element(a + 1, a + n + 1);
  long long res = 1000000000000000000;
  while (hi - lo > 1) {
    long long mid = (hi + lo) >> 1;
    if (f(mid) < f(mid + 1))
      hi = mid;
    else
      lo = mid;
  }
  res = min(res, f(lo - 1));
  res = min(res, f(lo));
  res = min(res, f(lo + 1));
  cout << res << endl;
}
