#include <bits/stdc++.h>
using namespace std;
const int N = 3000000;
const long long inf = 4e18;
int n, m, a[N];
long long dp1[N], dp2[N], kol1[N], kol2[N];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  long long kol = 0, val = 0;
  for (int i = 0; i <= n - 1; i++) {
    if (i % m == 0) {
      kol++;
      val += a[i + 1];
    }
    kol1[i + 1] = kol;
    dp1[i + 1] = val;
  }
  int j = 0;
  kol = 0;
  val = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (j % m == 0) {
      kol++;
      val += a[i + 1];
    }
    j++;
    kol2[i + 1] = kol;
    dp2[i + 1] = val;
  }
  long long ans = inf;
  for (int i = 1; i <= n; i++) {
    long long res = kol1[i] * a[i] - dp1[i] + dp2[i] - kol2[i] * a[i];
    ans = min(ans, 2ll * res);
  }
  cout << ans;
  return 0;
}
