#include <bits/stdc++.h>
using namespace std;
const long long N = 3e5, INF = 1e16;
long long a[N];
vector<long long> q[N];
int32_t main() {
  ios_base::sync_with_stdio(false);
  long long n, k, ans = INF;
  cin >> n >> k;
  for (long long i = 0; i < n; ++i) {
    cin >> a[i];
    long long x = a[i], cur = 0;
    while (x > 0) {
      q[x].push_back(cur);
      x /= 2;
      ++cur;
    }
  }
  for (long long i = 0; i < N; ++i) {
    if (q[i].size() >= k) {
      sort(q[i].begin(), q[i].end());
      long long sum = 0;
      for (long long j = 0; j < k; ++j) sum += q[i][j];
      ans = min(ans, sum);
    }
  }
  cout << ans;
}
