#include // #include using namespace std; // using namespace atcoder; using ll = long long; using ull = unsigned long long; using P = pair; #define rep(i,n) for(ll i = 0;i < (ll)n;i++) #define ALL(x) (x).begin(),(x).end() #define MOD 1000000007 int main(){ ll n,k; cin >> n >> k; ll r = k,x = n-k,l = 0; k = n-k; ll d = 0; ll res = 1ll << 60; vector a(n); rep(i,n)cin >> a[i],d += a[i]-a[0]; for(ll i = r;i < n;i++)d -= (a[i]-a[0])*2; res = d; for(ll i = 1;i < n;i++){ d += (i-2*(k-x))*(a[i]-a[i-1]); d -= (n-i-1-2*x)*(a[i]-a[i-1]); while(abs(a[i]-a[r]) < abs(a[i]-a[l])){ d += 2*(abs(a[i]-a[l])-abs(a[i]-a[r])); l++;r++; l %= n;r %= n; x--; } res = min(res,d); } cout << res << "\n"; return 0; }