#include <bits/stdc++.h>
using namespace std;
const long long INF = (long long)1e18 + 7LL;
int a[1000100];
int n, m;
inline void minimize(long long &x, const long long &y) {
  if (x > y) x = y;
}
void process(void) {
  scanf("%d%d", &n, &m);
  for (int i = (1); i <= (n); i = i + 1) scanf("%d", &a[i]);
  int l = 1 - m;
  int r = n;
  long long res = INF;
  long long sl = 0;
  long long sr = a[r];
  while (r - m > 1) {
    r -= m;
    sr += a[r];
  }
  for (int i = (1); i <= (n); i = i + 1) {
    while (l + m < i) {
      l += m;
      sl += a[l];
    }
    while (r <= i) {
      sr -= a[r];
      r += m;
    }
    long long tmp = 0;
    if (l >= 1) tmp += 1LL * ((l - 1) / m + 1) * a[i] - sl;
    if (r <= n) tmp += sr - 1LL * ((n - r) / m + 1) * a[i];
    minimize(res, tmp);
  }
  cout << res * 2;
}
int main(void) {
  process();
  return 0;
}
