#include <bits/stdc++.h>
using namespace std;
long long n, m, v[1000005], st[1000005], dr[1000005], ans = ~(1LL << 63), poz;
char buffer[1005];
void read(long long &x) {
  x = 0;
  char semn = '+';
  while (buffer[poz] < '0' || buffer[poz] > '9') {
    semn = buffer[poz];
    if (++poz == 1000) {
      fread(buffer, 1, 1000, stdin);
      poz = 0;
    }
  }
  while ('0' <= buffer[poz] && buffer[poz] <= '9') {
    x = x * 10 + buffer[poz] - '0';
    if (++poz == 1000) {
      fread(buffer, 1, 1000, stdin);
      poz = 0;
    }
  }
  if (semn == '-') x = -x;
}
int main() {
  fread(buffer, 1, 1000, stdin);
  read(n);
  read(m);
  for (int i = 1; i <= n; i++) read(v[i]);
  st[1] = 0;
  for (int i = 2; i <= n; i++)
    st[i] += st[i - 1] + ((m + i - 2) / m) * abs(v[i] - v[i - 1]);
  dr[n] = 0;
  for (int i = n - 1; i >= 1; i--)
    dr[i] += dr[i + 1] + ((m + n - i - 1) / m) * abs(v[i] - v[i + 1]);
  for (int i = 1; i <= n; i++) ans = min(ans, st[i] + dr[i]);
  printf("%I64d", 2 * ans);
}
