#include <bits/stdc++.h>
using namespace std;
long long int x[1000005], num[1000005], cost[1000005];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  long long int t, n, m, cnt, i, j, k, ans;
  cin >> n >> k;
  for (int i = int(0); i < int(n); i++) cin >> x[i];
  sort(x, x + n);
  ans = 0x3f3f3f3f3f3f3f3f;
  for (int i = int(0); i < int(n); i++) {
    m = x[i];
    cnt = 0;
    while (m) {
      num[m]++;
      cost[m] = cost[m] + cnt;
      if (num[m] >= k) ans = min(ans, cost[m]);
      cnt++;
      m /= 2;
    }
    cost[0] = cost[0] + cnt;
    num[0]++;
    if (num[0] >= k) ans = min(ans, cost[0]);
  }
  cout << ans << endl;
  return 0;
}
