#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int TESTS = 1;
  while (TESTS--) {
    int i, n, j, k;
    cin >> n >> k;
    vector<int> v(n);
    vector<vector<int> > a(200005);
    for (i = 0; i < n; i++) cin >> v[i];
    int ans = INT_MAX;
    for (i = 0; i < n; i++) {
      int t = v[i], s = 0;
      while (t) {
        a[t].push_back(s);
        s++;
        t /= 2;
      }
    }
    for (i = 0; i < 200005; i++) {
      if (a[i].size() >= k) {
        sort((a[i]).begin(), (a[i]).end());
        int sum = 0;
        for (j = 0; j < k; j++) sum += a[i][j];
        ans = min(ans, sum);
      }
    }
    cout << ans << endl;
  }
}
