#include <bits/stdc++.h>
using namespace std;
long long int num[1000111];
long long sumleft[1000111];
long long sumright[1000111];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%I64d", &num[i]);
  for (int i = 1; i <= n; i++)
    if ((i - 1) % m == 0) {
      sumleft[i] = sumleft[i - 1] + num[i];
    } else
      sumleft[i] = sumleft[i - 1];
  for (int i = n; i; i--)
    if ((n - i) % m == 0)
      sumright[i] = sumright[i + 1] + num[i];
    else
      sumright[i] = sumright[i + 1];
  long long minn = 0x3f3f3f3f3f3f3f3f;
  for (int i = 1; i <= n; i++) {
    long long l = sumleft[i - 1];
    long long r = sumright[i + 1];
    long long sum = num[i] * ceil((i - 1.0) / m);
    sum -= l;
    sum += r;
    sum -= num[i] * ceil((n - i + 0.0) / m);
    minn = min(minn, sum);
  }
  printf("%I64d\n", minn * 2);
}
