#include <bits/stdc++.h>
using namespace std;
map<int, vector<long long> > mp;
int main() {
  std::ios_base::sync_with_stdio(false);
  int n, k;
  long long m = 1e15, ans = 0;
  cin >> n >> k;
  long long a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mp[a[i]].push_back(0);
  }
  for (int i = 0; i < n; i++) {
    int t = 1;
    int f = 0;
    while (true) {
      mp[a[i] / 2].push_back(t);
      t++;
      a[i] /= 2;
      if (f == 1) {
        break;
      }
      if (a[i] == 0) {
        f = 1;
      }
    }
  }
  for (auto x : mp) {
    if (x.second.size() >= k) {
      sort(x.second.begin(), x.second.end());
      ans = 0;
      for (int i = 0; i < k; i++) {
        ans += x.second[i];
      }
      m = min(m, ans);
    }
  }
  cout << m;
}
