#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, k, x;
  cin >> n >> k;
  map<long long,
      priority_queue<long long, vector<long long>, greater<long long> > >
      pq;
  vector<long long> v;
  while (n--) {
    cin >> x;
    v.push_back(x);
  }
  for (auto j : v) {
    long long g = 0;
    while (j) {
      pq[j].push(g);
      g++;
      j >>= 1;
    }
  }
  long long ans = 1e18;
  for (auto t : pq) {
    if (t.second.size() >= k) {
      long long tt = k, val = 0;
      while (tt--) {
        val += t.second.top();
        t.second.pop();
      }
      ans = min(ans, val);
    }
  }
  cout << ans;
}
