#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> v;
long long f(long long x) {
  int cantmin = lower_bound(v.begin(), v.end(), x) - v.begin();
  int cantmax = upper_bound(v.begin(), v.end(), x) - v.begin();
  long long ans = 0;
  for (int i = 0; i < cantmin; i += m) {
    ans += (2 * (x - v[i]));
  }
  for (int i = n - 1; i >= cantmax; i -= m) {
    ans += (2 * (v[i] - x));
  }
  return ans;
}
int main() {
  scanf("%d%d", &n, &m);
  v.resize(n);
  for (int i = 0; i < n; i++) scanf("%d", &v[i]);
  int lo = 0, hi = n - 1;
  while ((hi - lo) > 2) {
    long long mi1 = (2 * lo + hi) / 3;
    long long mi2 = (lo + 2 * hi) / 3;
    if (f(v[mi1]) > f(v[mi2]))
      lo = mi1;
    else
      hi = mi2;
  }
  long long ans = (1e18);
  for (long long i = lo; i <= hi; i++) ans = min(ans, f(v[i]));
  cout << ans << '\n';
  return 0;
}
