#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
long long pre[N], suf[N];
long long a[N];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]);
  for (int i = 1; i <= n; i++)
    if (i <= m)
      pre[i] = a[i];
    else
      pre[i] = a[i] + pre[i - m];
  for (int i = n; i >= 1; i--)
    if (i + m > n)
      suf[i] = a[i];
    else
      suf[i] = a[i] + suf[i + m];
  long long ans = 1LL << 60;
  int i = 1;
  while (i <= n) {
    int j = i;
    while (j <= n && a[i] == a[j]) j++;
    long long sum = 0;
    long long times = (i - 1) / m;
    if (times) sum += times * a[i] - pre[1 + (times - 1) * m];
    if ((i - 1) % m) sum += a[i] - a[times * m + 1];
    i = j - 1;
    times = (n - i) / m;
    if (times) sum += suf[n - (times - 1) * m] - times * a[i];
    if ((n - i) % m) sum += a[i + (n - i) % m] - a[i];
    ans = min(ans, sum);
    i++;
  }
  cout << ans * 2 << endl;
  return 0;
}
