#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  int a[n];
  int maxx = 0;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] > maxx) maxx = a[i];
  }
  sort(a, a + n);
  vector<vector<int> > cnt(maxx + 1);
  for (int i = 0; i < n; i++) {
    int x = a[i], kol = 0;
    while (x > 0) {
      if (cnt[x].size() == 0)
        cnt[x].push_back(kol);
      else {
        int ind = cnt[x].size() - 1;
        cnt[x].push_back(cnt[x][ind] + kol);
      }
      x = x / 2;
      kol++;
    }
  }
  int minn = INF;
  for (int i = maxx; i >= 0; i--) {
    if (cnt[i].size() >= k && cnt[i][k - 1] < minn) minn = cnt[i][k - 1];
  }
  cout << minn;
}
