#include <bits/stdc++.h>
using namespace std;
vector<int> cost[200001];
int main() {
  cin.sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  int res = 1e9;
  for (int i = 1; i <= n; ++i) {
    int x;
    cin >> x;
    cost[x].push_back(0);
    int cst = 1;
    while (x) {
      x /= 2;
      cost[x].push_back(cst);
      ++cst;
    }
  }
  for (int i = 0; i <= 2e5; ++i) {
    sort(cost[i].begin(), cost[i].end());
    int an = 0;
    if (cost[i].size() >= k) {
      for (int j = 0; j < k; ++j) {
        an += cost[i][j];
      }
      res = min(res, an);
    }
  }
  cout << res;
}
