#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long n, k;
  cin >> n >> k;
  vector<long long> a(n);
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<vector<long long>> mp(2e5 + 5);
  for (long long i = 0; i < n; i++) {
    long long cnt = 0;
    long long temp = a[i];
    while (temp) {
      mp[temp].push_back(cnt);
      temp >>= 1;
      cnt++;
    }
  }
  long long ans = 1e9 + 2;
  for (long long i = 0; i < 2e5 + 5; i++) {
    if (mp[i].size() < k) continue;
    sort(mp[i].begin(), mp[i].end());
    long long temp = 0;
    for (long long j = 0; j < k; j++) temp += mp[i][j];
    ans = min(ans, temp);
  }
  cout << ans << endl;
}
