#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}
using ll = long long int;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  while (t-- > 0) {
    int n, k;
    cin >> n >> k;
    int a[n];
    for (int i = 0; i < n; i++) cin >> a[i];
    sort(a, a + n);
    vector<pair<int, int> > v(200001);
    vector<int> ans;
    int min1 = INT_MAX;
    for (int i = 0; i < n; i++) {
      int x = a[i];
      int count = 0;
      while (x) {
        v[x].first++;
        v[x].second += count;
        if (v[x].first == k) ans.push_back(v[x].second);
        count++;
        x /= 2;
      }
    }
    sort(ans.begin(), ans.end());
    cout << ans[0] << endl;
  }
  return 0;
}
