#include <bits/stdc++.h>
using namespace std;
long long a[1000005];
int main() {
  int n, m, i, j;
  while (scanf("%d%d", &n, &m) != EOF) {
    long long ans = 0;
    for (i = 0; i < n; i++) scanf("%I64d", &a[i]);
    int nm = n / (2 * m);
    int res = n % m;
    int pos;
    if ((n / m) % 2 == 1)
      pos = min(nm * m + res, n - 1);
    else
      pos = max(nm * m + res - 1, 0);
    i = j = 0;
    while (i < pos) {
      ans += 2 * abs(a[i] - a[pos]);
      i += m;
      i = min(i, pos);
    }
    i = n - 1;
    while (i > pos) {
      ans += 2 * abs(a[i] - a[pos]);
      i -= m;
      i = max(i, pos);
    }
    printf("%I64d\n", ans);
  }
  return 0;
}
