#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:667772160")
struct debugger {
  template <typename T>
  debugger& operator,(const T& v) {
    cerr << v << " ";
    return *this;
  }
} dbg;
long long arr[1000000 + 7], csum[1000000 + 7];
int main() {
  int n, cont = 0, k;
  scanf("%d", &n);
  scanf("%d", &k);
  for (int i = 0; i < n; i++) scanf("%lld", &arr[i]);
  for (int i = n - 1; i >= 0; i--) {
    if ((n - i) % k == 1 || k == 1)
      csum[i] = csum[i + 1] + arr[i];
    else
      csum[i] = csum[i + 1];
  }
  long long ans = 1e18, temp, total, cnsum = 0;
  for (int i = 0; i < n; i++) {
    temp = 0;
    total = (i + k - 1) / k;
    temp = (arr[i] * total - cnsum);
    total = (n - 1 - i + k - 1) / k;
    temp += (csum[i + 1] - arr[i] * total);
    ans = min(ans, temp);
    if (i % k == 0) cnsum += arr[i];
  }
  ans = ans * 2LL;
  printf("%lld\n", ans);
  return 0;
}
