#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
int n, m;
long long a[N];
long long L[N], R[N];
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; ++i) scanf("%I64d", a + i);
  for (int i = 1; i < n; ++i) {
    int pre = i / m * m;
    if (pre == 0)
      L[i] = 2 * abs(a[i] - a[pre]);
    else
      L[i] = L[pre - 1] + 1LL * (i / m) * 2 * abs(a[i] - a[pre - 1]) +
             2 * abs(a[i] - a[pre]);
  }
  reverse(a, a + n);
  for (int i = 1; i < n; ++i) {
    int pre = i / m * m;
    if (pre == 0)
      R[i] = 2 * abs(a[i] - a[pre]);
    else
      R[i] = R[pre - 1] + 1LL * (i / m) * 2 * abs(a[i] - a[pre - 1]) +
             2 * abs(a[i] - a[pre]);
  }
  reverse(R, R + n);
  long long ans = L[0] + R[0];
  for (int i = 1; i < n; ++i) {
    ans = min(ans, L[i] + R[i]);
  }
  cout << ans << endl;
  return 0;
}
