#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long n, k;
  cin >> n >> k;
  long long a[n];
  unordered_map<long long, long long> um, c;
  long long it = 0;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    um[a[i]]++;
    c[a[i]] += it;
    if (um[a[i]] == k) {
      cout << c[a[i]];
      return 0;
    }
  }
  long long ans = INT_MAX;
  while (um[0] < n) {
    it++;
    for (long long i = 0; i < n; i++) {
      if (a[i] == 0) continue;
      a[i] = a[i] / 2;
      um[a[i]]++;
      c[a[i]] += it;
      if (um[a[i]] == k) {
        ans = min(c[a[i]], ans);
      }
    }
  }
  cout << ans;
  return 0;
}
