#include <bits/stdc++.h>
using namespace std;
const int NS = 1100000;
const long long MX = 1000000000;
long long n, m, maxs, mins;
long long a[NS];
long long cal(long long p) {
  long long res = 0;
  int pos = 0;
  while (pos < n && a[pos] <= p) {
    res += (p - a[pos]) * 2;
    pos += m;
  }
  pos = n - 1;
  while (pos >= 0 && a[pos] >= p) {
    res += (a[pos] - p) * 2;
    pos -= m;
  }
  return res;
}
long long trisection() {
  long long res = MX * MX;
  long long m1, m2, v1, v2;
  long long up = maxs, down = mins;
  while ((up - down) > 3) {
    m1 = (up + down + down) / 3;
    m2 = (up + up + down) / 3;
    v1 = cal(m1), v2 = cal(m2);
    res = min(res, v1);
    res = min(res, v2);
    if (v1 > v2) {
      down = m1;
    } else {
      up = m2;
    }
  }
  while (up >= down) {
    res = min(res, cal(up));
    up--;
  }
  return res;
}
int main() {
  int ttttt;
  while (~scanf("%I64d %I64d", &n, &m)) {
    maxs = -MX;
    mins = MX;
    for (int i = 0; i < n; i++) {
      scanf("%I64d", &a[i]);
      maxs = max(maxs, a[i]);
      mins = min(mins, a[i]);
    }
    printf("%I64d\n", trisection());
  }
  return 0;
}
