#include <bits/stdc++.h>
using namespace std;
int a[1000010];
int n, m;
inline long long calc(int v) {
  long long res = 0;
  int cnt = 0;
  int st = n - 1;
  for (int i = n - 1; i >= v; --i) {
    cnt++;
    if ((i == v) || (cnt == m)) {
      res += (a[st] - a[v]);
      res += (a[st] - a[v]);
      st = i - 1;
      cnt = 0;
    }
  }
  cnt = 0;
  st = 0;
  for (int i = (0); i < (v); ++i) {
    cnt++;
    if ((i == v - 1) || (cnt == m)) {
      res += (a[v] - a[st]);
      res += (a[v] - a[st]);
      st = i + 1;
      cnt = 0;
    }
  }
  return res;
}
int main() {
  long long res = 1000000000000000000ll;
  scanf("%d%d", &n, &m);
  for (int i = (0); i < (n); ++i) {
    scanf("%d", &a[i]);
  }
  int l = 0, r = n - 1;
  while (r - l > 3) {
    int m1 = l + (r - l) / 3;
    int m2 = r - (r - l) / 3;
    long long val1 = calc(m1);
    long long val2 = calc(m2);
    if (val1 < val2) {
      r = m2;
      res = ((res < val1) ? (res) : (val1));
    } else {
      l = m1;
      res = ((res < val2) ? (res) : (val2));
    }
  }
  for (int i = (l); i < (r + 1); ++i) {
    long long val = calc(i);
    res = ((res < val) ? (res) : (val));
  }
  cout << res << endl;
}
