#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int INF = 1e9;
const int MOD = 1e9 + 7;
const int M = INF;
const double RRR = 180.0 / PI;
const long double EPS = 1e-8;
long long n, m;
vector<long long> mas;
long long func(long long x) {
  long long count = 0;
  long long res = 0;
  long long i;
  for (i = 0; i < n; i++) {
    if (mas[i] == x) {
      continue;
    }
    if (mas[i] > x) {
      break;
    }
    if (count == 0) {
      res += 2 * abs(x - mas[i]);
      count++;
    } else {
      count++;
    }
    if (count == m) {
      count = 0;
    }
  }
  count = 0;
  for (long long j = n - 1; j >= i; j--) {
    if (count == 0) {
      res += 2 * abs(x - mas[j]);
      count++;
    } else {
      count++;
    }
    if (count == m) {
      count = 0;
    }
  }
  return res;
}
int main() {
  double TIME_START = clock();
  cin >> n >> m;
  mas.resize(n);
  for (long long i = 0; i < n; i++) {
    int temp;
    scanf("%d", &temp);
    mas[i] = temp;
  }
  long long l = -1e10, r = 1e10;
  while (r - l >= 3) {
    long long m1 = l + (r - l) / 3;
    long long m2 = r - (r - l) / 3;
    if (func(m1) > func(m2)) {
      l = m1;
    } else {
      r = m2;
    }
  }
  long long ans = func(l);
  for (long long i = l; i <= r; i++) {
    ans = min(func(i), ans);
  }
  cout << ans << endl;
  return 0;
}
