#include <bits/stdc++.h>
using namespace std;
int vis[200005];
vector<int> v[200005];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  int ar[n + 2];
  for (int i = 0; i < n; i++) {
    cin >> ar[i];
  }
  for (int i = 0; i < n; i++) {
    int d = ar[i];
    int cnt = 0;
    while (d) {
      v[d].push_back(cnt);
      cnt++;
      d /= 2;
    }
    v[d].push_back(cnt);
  }
  int ans = INT_MAX;
  for (int i = 0; i <= 200000; i++) {
    if (v[i].size() < k) continue;
    sort(v[i].begin(), v[i].end());
    int sum = 0;
    for (int j = 0; j < k; j++) {
      sum += v[i][j];
    }
    ans = min(ans, sum);
  }
  cout << ans << "\n";
  return 0;
}
