#include <bits/stdc++.h>
using namespace std;
int t = 1;
vector<long long int> a, b;
int main() {
  long long int k, n;
  cin >> n >> k;
  vector<long long int> adj[200001];
  for (int i = 0; i < n; i++) {
    int v;
    cin >> v;
    int cost = 0;
    while (v > 0) {
      adj[v].push_back(cost);
      cost++;
      v /= 2;
    }
  }
  long long int ans = INT_MAX;
  for (int i = 0; i <= 1e5 * 2; i++) {
    if (adj[i].size() >= k) {
      sort(adj[i].begin(), adj[i].end());
      long long int sum = 0;
      for (long long int j = 0; j < k; j++) {
        sum += adj[i][j];
      }
      ans = min(ans, sum);
    }
  }
  cout << ans << endl;
}
