#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 100005;
const long long mod = 1e9 + 7;
vector<long long> v[2 * MAXN];
long long n, k;
long long ans = INT_MAX;
void unstopable() {
  cin >> n >> k;
  vector<long long> a(n);
  for (long long i = 0, ThxDem = n; i < ThxDem; i++) {
    cin >> a[i];
    long long cnt = 0;
    while (a[i]) {
      v[a[i]].push_back(cnt);
      cnt++;
      a[i] >>= 1;
    }
  }
  for (long long i = 0, ThxDem = 2 * MAXN; i < ThxDem; i++) {
    if (v[i].size() >= k) {
      sort(v[i].begin(), v[i].end());
      ans = min(ans, accumulate(v[i].begin(), v[i].begin() + k, 0LL));
    }
  }
  cout << ans;
}
signed main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  unstopable();
}
