#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-6;
const int MOD = 1e9 + 7;
const int MAXN = 1100001;
const double PI = acos(-1.0);
long long ipt[MAXN];
long long lsum[MAXN], rsum[MAXN];
long long sum[MAXN];
int main() {
  long long n, len;
  while (cin >> n >> len) {
    memset(sum, 0, sizeof(sum));
    memset(lsum, 0, sizeof(lsum));
    memset(rsum, 0, sizeof(rsum));
    for (int i = 0; i < (n); ++i) cin >> ipt[i];
    for (int i = (1); i < (n); ++i) {
      sum[i] += sum[i - 1] + ipt[i] - ipt[i - 1];
    }
    long long group = 0;
    for (long long i = 0; i + len < n; i += len) {
      long long l = len * group, r = l + len;
      if (group) lsum[group] = lsum[group - 1];
      lsum[group] += (sum[r] - sum[l]) * (group + 1);
      group++;
    }
    group = 0;
    for (long long i = n - 1; i - len >= 0; i -= len) {
      long long r = len * group, l = r + len;
      l = n - 1 - l;
      r = n - 1 - r;
      if (group) rsum[group] = rsum[group - 1];
      rsum[group] += (sum[r] - sum[l]) * (group + 1);
      group++;
    }
    long long ans = 1e18;
    for (long long i = 0; i < n; i++) {
      long long t = 0;
      if (i / len > 0) t += lsum[i / len - 1] * 2;
      if ((n - 1 - i) / len > 0) t += rsum[(n - 1 - i) / len - 1] * 2;
      t += (sum[i] - sum[i / len * len]) * 2 * (i / len + 1);
      t += (sum[n - 1 - (n - 1 - i) / len * len] - sum[i]) * 2 *
           ((n - 1 - i) / len + 1);
      ans = min(ans, t);
    }
    cout << ans << endl;
  }
  return 0;
}
