#include <bits/stdc++.h>
using namespace std;
const long long N = 200005, INF = 2000000000000000000;
long long power(long long a, long long b, long long p) {
  if (a == 0) return 0;
  long long res = 1;
  a %= p;
  while (b > 0) {
    if (b & 1) res = (res * a) % p;
    b >>= 1;
    a = (a * a) % p;
  }
  return res;
}
int32_t main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout.precision(std::numeric_limits<double>::max_digits10);
  ;
  long long n, k;
  cin >> n >> k;
  vector<long long> v[N];
  long long ar[n];
  for (long long i = 0; i < n; i++) cin >> ar[i];
  for (long long i = 0; i < n; i++) {
    v[ar[i]].push_back(0);
    long long x = 0;
    while (ar[i] > 1) {
      x++;
      ar[i] /= 2;
      v[ar[i]].push_back(x);
    }
  }
  long long ans = INF;
  for (long long i = 1; i < N; i++) {
    if (v[i].size() >= k) {
      sort((v[i]).begin(), (v[i]).end());
      long long c = 0;
      for (long long j = 0; j < k; j++) c += v[i][j];
      ans = min(ans, c);
    }
  }
  cout << ans;
}
