#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
vector<int> G[maxn];
int n, m, k, x;
signed main() {
  ios::sync_with_stdio(0), cin.tie(0);
  ;
  cin >> n >> k;
  for (int i = 1, x; i <= n; i++) {
    cin >> m;
    x = m;
    int cnt = 0;
    while (x) {
      G[x].push_back(cnt);
      cnt++;
      x >>= 1;
    }
  }
  int ans = 0x3f3f3f3f;
  for (int i = 1; i <= maxn - 5; i++) {
    if (G[i].size() < k)
      continue;
    else {
      sort(G[i].begin(), G[i].end());
      int tmp = 0;
      for (int j = 0; j < k; j++) tmp += G[i][j];
      ans = min(tmp, ans);
    }
  }
  cout << ans << endl;
  return 0;
}
