#include <bits/stdc++.h>
using namespace std;
const int N = 1000100;
long long ans = 1e18;
int n, m, a[N], b[N];
set<int> s;
long long calc(int mid) {
  long long ans = 0;
  mid = lower_bound(a, a + n, b[mid]) - a;
  for (int i = n - 1; i > mid; i -= m) ans += 2LL * (a[i] - a[mid]);
  for (int i = 0; i < mid; i += m) ans += 2LL * (a[mid] - a[i]);
  return ans;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    s.insert(a[i]);
  }
  int cnt = 0;
  for (auto x : s) b[cnt++] = x;
  if (cnt == 1) {
    printf("0");
    return 0;
  }
  int low = 0, high = cnt - 1;
  while (high > low) {
    int mid = (high + low) / 2;
    long long ans1 = calc(mid), ans2 = calc(mid + 1);
    ans = min({ans, ans1, ans2});
    if (ans1 < ans2)
      high = mid - 1;
    else
      low = mid + 1;
  }
  printf("%lld", ans);
}
