#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int a[N];
vector<int> ve[N];
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) {
    int cnt = 0;
    int t = a[i];
    ve[t].push_back(0);
    while (t) {
      cnt++;
      t >>= 1;
      ve[t].push_back(cnt);
    }
  }
  int ans = n * n + 5;
  for (int i = 0; i < N; i++) {
    if (ve[i].size() < k) continue;
    sort(ve[i].begin(), ve[i].end());
    int cnt = 0;
    for (int j = 0; j < k; j++) cnt += ve[i][j];
    ans = min(ans, cnt);
  }
  cout << ans << endl;
  return 0;
}
