#include <bits/stdc++.h>
using namespace std;
const int inf = 0x20202020;
const int mod = 1000000007;
inline void read(int &x) {
  char ch;
  bool f = 0;
  int a = 0;
  while (!((((ch = getchar()) >= '0') && (ch <= '9')) || (ch == '-')))
    ;
  if (ch != '-')
    a *= 10, a += ch - '0';
  else
    f = 1;
  while (((ch = getchar()) >= '0') && (ch <= '9')) a *= 10, a += ch - '0';
  if (f) a = -a;
  x = a;
}
const int DX[] = {1, 0, -1, 0}, DY[] = {0, 1, 0, -1};
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
long long powmod(long long a, long long b, long long mod) {
  long long res = 1;
  a %= mod;
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
const int N = 1111111, M = 111111;
int l, m, n, t, a[N];
double dis(double x) {
  double ans = 0;
  int p = 1, cnt = 0;
  while (a[p] <= x && p <= n) {
    if (cnt == 0) cnt = m, ans += x - a[p];
    cnt--;
    p++;
  }
  p = n;
  cnt = 0;
  while (a[p] >= x && p >= 1) {
    if (cnt == 0) cnt = m, ans += a[p] - x;
    cnt--;
    p--;
  }
  return 2 * ans;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  double Min = -1e9, Max = 1e9;
  while (Min + 1e-3 < Max) {
    double mid1 = (2 * Min + Max) / 3, mid2 = (Min + 2 * Max) / 3;
    if (dis(mid1) < dis(mid2))
      Max = mid2;
    else
      Min = mid1;
  }
  printf("%.0lf\n", min(dis((int)Min), dis((int)Min + 1)));
  return 0;
}
