#include <bits/stdc++.h>
using namespace std;
long long int me(long long int x, long long int n) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return me((x * x) % 1000000007, n / 2);
  else
    return (x * me((x * x) % 1000000007, (n - 1) / 2)) % 1000000007;
}
bool IsPrime(long long int n) {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 == 0 || n % 3 == 0) return false;
  for (long long int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0) return false;
  return true;
}
void solve() {
  long long int n, k;
  cin >> n >> k;
  long long int a[n];
  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
  };
  sort(a, a + n);
  map<long long int, vector<long long int> > m;
  long long int ans = 1e15;
  for (long long int i = 0; i < n; i++) {
    long long int p = a[i];
    long long int cnt = 0;
    m[p].push_back(0);
    while (p > 0) {
      p /= 2;
      cnt++;
      m[p].push_back(cnt);
    }
  }
  for (auto it : m) {
    long long int cnt = 0;
    if (it.second.size() >= k) {
      for (long long int i = 0; i < k; i++) cnt += it.second[i];
      ans = min(ans, cnt);
    }
  }
  cout << ans << endl;
}
signed main() {
  ios_base::sync_with_stdio(false);
  long long int t = 1;
  while (t--) solve();
  return 0;
}
