#include <bits/stdc++.h>
using namespace std;
int n, m;
long long s[1000005], l[1000005], r[1000005];
long long ans = (1LL << 60);
int main() {
  scanf("%d %d", &n, &m);
  for (int i = (1); i <= (n); i++) scanf("%lld", &s[i]);
  m = min(m, n);
  for (int i = (1); i <= (n); i++) {
    l[i] = l[i - 1];
    if ((i - 1) % m == 0) l[i] += s[i];
  }
  for (int i = (n); i >= (1); i--) {
    r[i] = r[i + 1];
    if ((n - i) % m == 0) r[i] += s[i];
  }
  for (int i = (1); i <= (n); i++) {
    int cl = (i - 1) / m + 1;
    int cr = (n - i) / m + 1;
    long long cost = s[i] * cl - l[i] + r[i] - s[i] * cr;
    ans = min(ans, cost);
  }
  printf("%lld\n", 2 * ans);
  return 0;
}
