#include <bits/stdc++.h>
#pragma comment(linker, "/stack:30000000")
using namespace std;
const int Direction[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
const int Nmax = 2000100;
long long a[Nmax];
long long Pref[Nmax], Suff[Nmax];
long long C1[Nmax], C2[Nmax];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i < (int)(n + 1); ++i) scanf("%I64d", &a[i]);
  int mn = a[1];
  for (int i = 1; i < (int)(n + 1); ++i) a[i] -= mn;
  Pref[0] = 0;
  C1[0] = 0;
  for (int i = 1; i < (int)(n + 1); ++i)
    Pref[i] = Pref[i - 1] + ((i - 1) % m == 0 ? a[i] : 0),
    C1[i] = C1[i - 1] + ((i - 1) % m == 0 ? 1 : 0);
  Suff[n + 1] = 0;
  for (int i = n; i >= 1; i--)
    Suff[i] = Suff[i + 1] + ((i % m == n % m) ? a[i] : 0),
    C2[i] = C2[i + 1] + ((i % m == n % m) ? 1 : 0);
  long long Ans = 4000000000000000000LL;
  for (int i = 1; i <= n; i++) {
    int j = i;
    while (j <= n && a[j] == a[i]) j++;
    j--;
    long long Temp = 0;
    Temp = (C1[i - 1] * 1ll * a[i] - Pref[i - 1]);
    Temp = 2 * (Temp + (Suff[j + 1] - C2[j + 1] * a[j]));
    Ans = min(Ans, Temp);
    i = j;
  }
  cout << Ans << endl;
  return 0;
}
