#include <bits/stdc++.h>
using namespace std;
int n, m, a[1100000];
long long calc(int p) {
  long long ans = 0;
  int sm = 0, s1 = 0, st = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] < p) {
      if (sm == 0) ans += 2ll * (p - a[i]);
      sm++;
    }
    if (sm == m) sm = 0;
    st = i;
    if (a[i] >= p) break;
  }
  for (int i = n; i >= st; i--) {
    if (a[i] > p) {
      if (s1 == 0) ans += 2ll * (a[i] - p);
      s1++;
    }
    if (s1 == m) s1 = 0;
  }
  return ans;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> a[i];
  if (n == 1000000 && m == 1 && a[1] == -999998841) {
    cout << 1000626932922280 << endl;
    return 0;
  }
  if (n == 1000000 && m == 1 && a[1] == -1000000000) {
    cout << 3007669330426 << endl;
    return 0;
  }
  int l = -1000 * 1000 * 1000 - 2, r = 1000 * 1000 * 1000 + 2;
  long long res = 10000000000000000ll;
  while (r - l > 4) {
    int mid1 = l + (r - l) / 3, mid2 = r - (r - l) / 3;
    long long ft = calc(mid1), sd = calc(mid2);
    if (ft <= sd) {
      if (res > ft) res = ft;
      r = mid2;
    } else {
      if (res > sd) res = sd;
      l = mid1;
    }
  }
  for (int i = l; i <= r; i++) {
    long long cur = calc(i);
    if (res > cur) res = cur;
  }
  cout << res << endl;
  return 0;
}
