Problem: You are given the array a consisting of n elements and the integer k ≤ n.

You want to obtain at least k equal elements in the array a. In one move, you can make one of the following two operations:

  * Take one of the minimum elements of the array and increase its value by one (more formally, if the minimum value of a is mn then you choose such index i that a_i = mn and set a_i := a_i + 1); 
  * take one of the maximum elements of the array and decrease its value by one (more formally, if the maximum value of a is mx then you choose such index i that a_i = mx and set a_i := a_i - 1). 



Your task is to calculate the minimum number of moves required to obtain at least k equal elements in the array.

Input

The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a and the required number of equal elements.

The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is the i-th element of a.

Output

Print one integer — the minimum number of moves required to obtain at least k equal elements in the array.

Examples

Input


6 5
1 2 2 4 2 3


Output


3


Input


7 5
3 3 2 1 1 1 3


Output


4

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
template <class n, class second>
ostream &operator<<(ostream &p, pair<n, second> x) {
  return p << "<" << x.first << ", " << x.second << ">";
}
template <class n>
auto operator<<(ostream &p, n y) ->
    typename enable_if<!is_same<n, string>::value,
                       decltype(y.begin(), p)>::type {
  long long o = 0;
  p << "{";
  for (auto c : y) {
    if (o++) p << ", ";
    p << c;
  }
  return p << "}";
}
void dor() { cerr << '\n'; }
template <class n, class... second>
void dor(n p, second... y) {
  cerr << p << " ";
  dor(y...);
}
template <class n, class second>
void mini(n &p, second y) {
  if (p > y) p = y;
}
template <class n, class second>
void maxi(n &p, second y) {
  if (p < y) p = y;
}
const long long MXN = 2e5 + 5;
long long t[MXN];
long long sum[MXN];
long long sum_seg(long long a, long long b) { return sum[b] - sum[a - 1]; }
void solve() {
  long long n, k;
  cin >> n >> k;
  map<long long, long long> cnt;
  vector<long long> V;
  for (long long i = (1); i <= (long long)(n); ++i) {
    long long a;
    cin >> a;
    cnt[a]++;
    t[i] = a;
    if (cnt[a] >= k) {
      cout << 0 << '\n';
      return;
    }
  }
  long long res = 1e18;
  long long mi = cnt.begin()->first;
  long long mx = (--cnt.end())->first;
  long long ile = 0;
  long long cost = 0;
  long long prev = mi;
  for (auto i : cnt) {
    cost += (long long)ile * (i.first - prev);
    ile += i.second;
    if (ile >= k) {
      mini(res, cost);
    }
    prev = i.first;
  }
  ile = 0;
  cost = 0;
  prev = mx;
  for (auto iter = cnt.rbegin(); iter != cnt.rend(); ++iter) {
    auto i = *iter;
    cost += (long long)ile * (prev - i.first);
    ile += i.second;
    if (ile >= k) {
      mini(res, cost);
    }
    prev = i.first;
  }
  sort(t + 1, t + n + 1);
  for (long long i = (1); i <= (long long)(n); ++i) {
    sum[i] = sum[i - 1] + t[i];
  }
  for (long long i = (1); i <= (long long)(n); ++i) {
    long long cnt_left = i;
    long long cnt_right = n - i;
    if (t[i] == t[i + 1]) continue;
    long long cost = (long long)t[i] * (cnt_left)-sum_seg(1, i) +
                     sum_seg(i + 1, n) - ((long long)t[i] + 1) * cnt_right;
    mini(res, cost + max(0LL, k - max(cnt_right, cnt_left)));
  }
  for (long long i = (2); i <= (long long)(n); ++i) {
    long long cnt_left = i - 1;
    long long cnt_right = n - i + 1;
    if (t[i - 1] == t[i]) continue;
    long long cost = (long long)(t[i] - 1) * (cnt_left)-sum_seg(1, i - 1) +
                     sum_seg(i, n) - ((long long)t[i]) * cnt_right;
    mini(res, cost + max(0LL, k - max(cnt_right, cnt_left)));
  }
  for (long long i = (1); i <= (long long)(n); ++i) {
    long long cnt_left = i;
    long long cnt_right = n - i;
    long long cost = (long long)t[i] * (cnt_left)-sum_seg(1, i) +
                     sum_seg(i + 1, n) - (long long)t[i] * cnt_right;
    mini(res, cost);
  }
  long long rowne_lewo = 1;
  for (long long i = (1); i <= (long long)(n); ++i) {
    long long cnt_right = n - i;
    if (t[i] == t[i + 1]) {
      rowne_lewo++;
      continue;
    }
    long long cost = sum_seg(i + 1, n) - (t[i] + 1) * cnt_right;
    if (rowne_lewo + cnt_right >= k) mini(res, cost + max(0LL, k - rowne_lewo));
    rowne_lewo = 1;
  }
  long long rowne_prawo = 1;
  for (long long i = (n); i >= (long long)(1); --i) {
    long long cnt_left = i - 1;
    if (t[i] == t[i - 1]) {
      rowne_prawo++;
      continue;
    }
    long long cost = (t[i] - 1) * cnt_left - sum_seg(1, i - 1);
    if (rowne_prawo + cnt_left >= k)
      mini(res, cost + max(0LL, k - rowne_prawo));
    rowne_prawo = 1;
  }
  cout << res << '\n';
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  solve();
}