#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[]) {
  vector<vector<int> > DATA(2123456);
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    int cnt = 0;
    while (x > 0) {
      DATA[x].push_back(cnt);
      cnt++;
      x /= 2;
    }
    DATA[0].push_back(cnt);
  }
  int answer = 987654321;
  for (int i = 0; i <= 2123456; i++) {
    if (DATA[i].size() < k) {
      continue;
    }
    sort(DATA[i].begin(), DATA[i].end());
    int answer1 = 0;
    for (int j = 0; j < k; j++) {
      answer1 += DATA[i][j];
    }
    answer = min(answer, answer1);
  }
  cout << answer;
  return 0;
}
