#include <bits/stdc++.h>
using namespace std;
const int MAX = 2000000;
int A[MAX], med, m, n;
long long ans = -1;
void solve() {
  int i, j, k;
  long long cur = 0;
  for (i = 0; i < med; i += m) {
    cur += 2ll * (A[med] - A[i]);
  }
  for (i = n - 1; i > med; i -= m) {
    cur += 2ll * (A[i] - A[med]);
  }
  if (ans == (-1) || cur < ans) ans = cur;
}
int main() {
  int i, j, k;
  while (cin >> n >> m) {
    ans = -1;
    for (int i = 0; i < n; i++) scanf("%d", A + i);
    if (n % 2) {
      med = n / 2;
      solve();
    } else {
      med = n / 2;
      solve();
      med = n / 2 + 1;
      solve();
    }
    if (n == 2)
      cout << ((long long)2) * (A[1] - A[0]) << endl;
    else
      cout << ans << endl;
  }
}
