#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 55;
int n, k;
vector<pair<int, int>> a;
vector<int> x[N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  a = vector<pair<int, int>>(n);
  for (auto& x : a) cin >> x.first;
  for (int i = 0; i < n; ++i) {
    int c = a[i].first;
    int cnt = 0;
    while (c) {
      a.emplace_back(c /= 2, ++cnt);
    }
  }
  for (auto z : a) x[z.first].push_back(z.second);
  int ans = N * 100;
  for (int i = 0; i < N; ++i) {
    sort(x[i].begin(), x[i].end());
    if (x[i].size() < k) continue;
    int aa = 0;
    for (int j = 0; j < k; ++j) aa += x[i][j];
    ans = min(ans, aa);
  }
  cout << ans << '\n';
  return 0;
}
