#include <bits/stdc++.h>
using namespace std;
long long cnt[3000000];
long long cost[3000000];
int main() {
  long long n, k;
  cin >> n >> k;
  long long ans = 1e9;
  vector<long long> inputs;
  for (long long i = 0; i < n; ++i) {
    long long a;
    cin >> a;
    inputs.push_back(a);
  }
  sort(inputs.begin(), inputs.end());
  for (long long i = 0; i < n; ++i) {
    long long a = inputs[i];
    long long now_cost = 0;
    while (a != 0) {
      cnt[a]++;
      cost[a] += now_cost;
      if (cnt[a] >= k) {
        ans = min(ans, cost[a]);
      }
      now_cost++;
      a /= 2;
    }
  }
  cout << ans << endl;
}
