#include <bits/stdc++.h>
int n, m, i, fl[1200000], fr[1200000];
long long left, right, ch, sum, min, a[1200000];
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) scanf("%I64d", &a[i]);
  for (i = 2; i <= n; i++)
    if (a[i] == a[i - 1])
      fl[i] = fl[i - 1];
    else
      fl[i] = i - 1;
  for (i = n - 1; i >= 1; i--)
    if (a[i] == a[i + 1])
      fr[i] = fr[i + 1];
    else
      fr[i] = n - i;
  sum = 0;
  for (i = n; i >= 1; i -= m) sum += (a[i] - a[1]) * 2;
  min = sum;
  for (i = 2; i <= n; i++)
    if (a[i] != a[i - 1]) {
      left = ceil(fl[i] / (m * 1.0));
      right = ceil(fr[i - 1] / (m * 1.0));
      ch = a[i] - a[i - 1];
      sum = sum + 2 * (left * ch - right * ch);
      if (sum < min) min = sum;
    }
  printf("%I64d\n", min);
}
