#include <bits/stdc++.h>
using namespace std;
long long v[1000000];
int n, m;
long long calc(long long pos) {
  int i = 0, j = n - 1;
  long long ret = 0;
  while (i < n and v[i] < pos) {
    ret += 2 * (pos - v[i]);
    i += m;
  }
  while (j >= 0 and v[j] > pos) {
    ret += 2 * (v[j] - pos);
    j -= m;
  }
  return ret;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%lld", &v[i]);
  if (n % 2)
    printf("%lld\n", calc(v[n / 2]));
  else
    printf("%lld\n", min(calc(v[n / 2]), calc(v[n / 2 - 1])));
  return 0;
}
