#include <bits/stdc++.h>
using namespace std;
int n, k, arr[200000 + 5], fr[200000 + 5], steps[200000 + 5], cop, cnt, ans;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  sort(arr + 1, arr + n + 1);
  for (int i = 1; i <= n; i++) {
    cop = arr[i];
    cnt = 0;
    while (cop != 0 && fr[cop] < k) {
      fr[cop]++;
      steps[cop] += cnt;
      cnt++;
      cop /= 2;
    }
    if (fr[0] < k) {
      fr[0]++;
      steps[0] += cnt;
    }
  }
  ans = 0;
  for (int i = 1; i <= arr[n]; i++) {
    if (fr[i] == k) {
      if (steps[ans] > steps[i]) {
        ans = i;
      }
    }
  }
  cout << steps[ans];
}
