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