#include <bits/stdc++.h>
using namespace std;
const long long NMAX = 205555;
long long dp[NMAX];
long long dp2[NMAX];
signed main() {
  ios_base::sync_with_stdio(false);
  long long n, k;
  long long res = INT_MAX;
  cin >> n >> k;
  vector<long long> a(n);
  for (long long &x : a) cin >> x;
  sort(a.begin(), a.end());
  for (long long i = 0; i < n; i++) {
    long long loc_res = 0;
    long long cp = a[i];
    dp[cp]++;
    while (cp != 0) {
      cp /= 2;
      loc_res++;
      if (dp[cp] < k) {
        dp2[cp] += loc_res;
      }
      dp[cp]++;
    }
  }
  for (long long i = 0; i < NMAX; i++) {
    if (dp[i] < k) continue;
    res = min(res, dp2[i]);
  }
  cout << res;
  return 0;
}
