#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; k = n-k; ll r = n-k,x = k,l = 0; ll d = 0; 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; r %= n; ll res = d; if(x == n)x--; for(ll i = 1;i < n;i++){ d -= (n-i-2*x)*(a[i]-a[i-1]); d += (i-2*(k-x))*(a[i]-a[i-1]); while(abs(a[i]-a[r]) < abs(a[i]-a[l]) && x > 0){ 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; }