#include <bits/stdc++.h>
using namespace std;
vector<long long> a;
stack<int> pos;
const long long INF = 1e18;
int main() {
  int n, m;
  cin >> n >> m;
  a.resize(n);
  for (int i = 0; i < n; i++) scanf("%I64d", &a[i]);
  long long sum = 0;
  long long to_cnt = 0;
  for (int i = n - 1; i > 0; i -= m) sum += a[i] - a[0], to_cnt++, pos.push(i);
  long long cnt = 0;
  long long Mi = sum;
  for (int i = 1; i < n; i++) {
    if (i % m == 1 || m == 1) cnt++;
    sum += cnt * (a[i] - a[i - 1]);
    sum -= to_cnt * (a[i] - a[i - 1]);
    if (pos.top() == i) {
      to_cnt--;
      pos.pop();
    }
    Mi = min(Mi, sum);
  }
  cout << 2 * Mi;
  return 0;
}
