#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
int32_t main() {
  long long n, k;
  cin >> n >> k;
  vector<long long> a(n);
  for (long long i = 0; i < n; i++) cin >> a[i];
  map<long long, multiset<long long> > m;
  for (long long i = 0; i < n; i++) {
    long long temp = a[i];
    long long cnt = 0;
    m[temp].insert(cnt);
    while (temp) {
      temp /= 2;
      cnt++;
      m[temp].insert(cnt);
    }
  }
  long long ans = INT_MAX;
  for (auto x : m) {
    if (x.second.size() >= k) {
      long long cnt = 0;
      long long t = 0;
      for (auto y : x.second) {
        if (t >= k) break;
        cnt += y;
        t++;
      }
      ans = min(ans, cnt);
    }
  }
  cout << ans << '\n';
  return 0;
}
