#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t;
  t = 1;
  while (t--) {
    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);
    long long int ans = 1000000007;
    map<long long int, long long int> m;
    map<long long int, long long int> co;
    for (long long int i = 0; i < n; i++) {
      long long int temp = a[i], steps = 0;
      while (temp >= 0) {
        m[temp]++;
        if (m[temp] <= k) co[temp] += steps;
        steps++;
        if (temp == 0) break;
        temp = temp >> 1;
      }
    }
    for (auto it = m.begin(); it != m.end(); it++) {
      if (it->second >= k) {
        ans = min(ans, co[it->first]);
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
