#include <bits/stdc++.h>
using namespace std;
const int sz = 2e5 + 10;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  int a;
  int cnt[sz] = {0};
  vector<vector<int>> org(sz);
  vector<int> p(n);
  for (int &x : p) cin >> x;
  sort(p.begin(), p.end());
  for (int i = 0; i < n; ++i) {
    a = p[i];
    cnt[a]++;
    int j = 1;
    while (a > 0) {
      a = a / 2;
      if (org[a].size() == 0)
        org[a].push_back(j);
      else {
        int f = org[a].size();
        int c = org[a][f - 1];
        org[a].push_back(c + j);
      }
      j++;
    }
  }
  int op = INT_MAX, pos;
  for (int i = 1; i < sz; ++i) {
    if (cnt[i] >= k) {
      cout << 0;
      return 0;
    }
    if (cnt[i] + org[i].size() >= k) {
      if (op > org[i][k - cnt[i] - 1]) op = org[i][k - cnt[i] - 1];
    }
  }
  cout << op;
  return 0;
}
