#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 7;
vector<int> v[N];
void solve() {
  int n, k, x;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (auto i : a) {
    x = i;
    int cnt = 0;
    while (x > 0) {
      v[x].push_back(cnt);
      x /= 2;
      ++cnt;
    }
  }
  int mn = 1e9;
  for (int i = 0; i <= N - 7; ++i) {
    sort(v[i].begin(), v[i].end());
    if ((int)v[i].size() >= k) {
      int sum = 0;
      for (int j = 0; j < k; ++j) {
        sum += v[i][j];
      }
      mn = min(mn, sum);
    }
  }
  cout << mn;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tt = 1;
  while (tt--) {
    solve();
  }
  return 0;
}
