#include <bits/stdc++.h>
using namespace std;
struct debugger {
  template <typename T>
  debugger& operator,(const T& v) {
    cerr << v << " ";
    return *this;
  }
} debug1;
template <typename T1, typename T2>
inline ostream& operator<<(ostream& os, const pair<T1, T2>& p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <typename T>
inline ostream& operator<<(ostream& os, const vector<T>& v) {
  bool first = true;
  os << "[";
  for (typename vector<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) {
    if (!first) os << ", ";
    os << *ii;
    first = false;
  }
  return os << "]";
}
const int NN = 1123456;
long long dp1[NN], dp2[NN];
int pos[NN];
void solve() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) scanf("%d", &pos[i]);
  long long sum, prev, no;
  dp2[n] = 0;
  sum += pos[n];
  no = 1LL;
  prev = n;
  for (int i = n - 1; i >= 1; --i) {
    if (i + m == prev) {
      sum += (long long)pos[i], ++no;
      prev = i;
    }
    dp2[i] = 2LL * (sum - (no * (long long)pos[i]));
  }
  dp1[1] = 0LL;
  sum = pos[1];
  no = 1LL;
  prev = 1;
  for (int i = 2; i <= n; ++i) {
    if (i - m == prev) {
      sum += (long long)pos[i], ++no;
      prev = i;
    }
    dp1[i] = 2LL * ((no * (long long)pos[i]) - sum);
  }
  long long ans = numeric_limits<long long>::max();
  for (int i = 1; i <= n; ++i) {
    long long res = dp1[i] + dp2[i];
    ans = min(ans, res);
  }
  printf("%lld\n", ans);
}
int main() {
  solve();
  return 0;
}
