#include <bits/stdc++.h>
int a[1010101];
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; ++i) scanf("%d", a + i);
  long long ans = 0;
  if (n & 1) {
    int h = n / 2 + 1;
    for (int i = 1; i < h; i += m) ans += a[h] - a[i];
    for (int i = n; i > h; i -= m) ans += a[i] - a[h];
  } else {
    int hl = n / 2;
    int hr = hl + 1;
    int x = (a[hl] + a[hr]) / 2;
    for (int i = 1; i <= hl; i += m) ans += x - a[i];
    for (int i = n; i >= hr; i -= m) ans += a[i] - x;
  }
  printf("%I64d", ans << 1);
}
