#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[1000000];
long long cal(int l, int r) {
  if (l > r) return 0;
  int len = r - l + 1;
  if (len <= m) return (long long)(a[r] - a[l]);
  return (long long)(a[r] - a[l]) + cal(l + m, r - m);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) scanf("%d", a + i);
  printf("%I64d\n", cal(0, n - 1) * 2);
  return 0;
}
