#include <bits/stdc++.h>
using namespace std;
int x[1000005];
long long L[1000005], R[1000005];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) scanf("%d", x + i);
  for (int i = 0; i < n; ++i) {
    if (i % m == 0)
      L[i] = L[i - 1] + x[i];
    else
      L[i] = L[i - 1];
  }
  for (int i = n - 1; i >= 0; --i) {
    if ((n - 1 - i) % m == 0)
      R[i] = R[i + 1] + x[i];
    else
      R[i] = R[i + 1];
  }
  long long ans = 1LL << 60;
  for (int i = 0; i < n; ++i) {
    long long tmp = 0;
    if (i) tmp += 1LL * (i + m - 1) / m * x[i] - L[i - 1];
    if (i + 1 < n) tmp += R[i + 1] - 1LL * (n - i + m - 2) / m * x[i];
    ans = min(ans, tmp);
  }
  printf("%I64d\n", ans * 2);
  return 0;
}
