#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
vector<int> v[N];
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ;
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) {
    int now = a[i], cur = 0;
    while (now) {
      v[now].push_back(cur);
      now /= 2;
      cur++;
    }
  }
  int ans = 2e9, cur;
  for (int i = 1; i <= 2e5 + 1; i++) {
    if (v[i].size() >= k) {
      cur = 0;
      sort(v[i].begin(), v[i].end());
      for (int j = 0; j < k; j++) cur += v[i][j];
    }
    ans = min(ans, cur);
  }
  cout << ans << endl;
  return 0;
}
