#include <bits/stdc++.h>
using namespace std;
long long a[1000100];
int main() {
  int i;
  long long sum = 0, n, m, xl;
  scanf("%I64d%I64d", &n, &m);
  for (i = 1; i <= n; i++) scanf("%I64d", &a[i]);
  if (n % 2 == 0)
    xl = (a[n / 2] + a[n / 2 + 1]) / 2;
  else
    xl = a[n / 2 + 1];
  for (i = 1; i <= n; i += m) {
    if (a[i] <= xl) sum += (abs(xl - a[i]) * 2);
  }
  for (i = n; i >= 1; i -= m) {
    if (a[i] >= xl) sum += (abs(xl - a[i]) * 2);
  }
  printf("%I64d\n", sum);
}
