#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const long long INF = 1000 * 1000 * 1000 + 7;
const long long LINF = (long long)INF * INF;
int n, m;
long long a[1 << 20];
long long get(int pos) {
  long long ans = 0;
  for (int i = n - 1; i >= 0; i -= m) {
    if (a[i] < pos) break;
    ans += 2 * (a[i] - pos);
  }
  for (int i = 0; i < n; i += m) {
    if (a[i] > pos) break;
    ans += 2 * (pos - a[i]);
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = (0); i < (n); ++i) cin >> a[i];
  long long ans = LINF * 8;
  long long L = 0, R = n - 1, M1, M2;
  while (R - L > 3) {
    M1 = L + (R - L) / 3;
    M2 = R - (R - L) / 3;
    long long x1 = get(a[M1]);
    long long x2 = get(a[M2]);
    ans = min(ans, x1);
    ans = min(ans, x2);
    if (x1 > x2)
      L = M1;
    else
      R = M2;
  }
  for (int i = (L); i < (R + 1); ++i) ans = min(ans, get(a[i]));
  cout << ans;
  return 0;
  return 0;
}
