#include <bits/stdc++.h>
using namespace std;
int a[200005];
vector<int> b[200005];
int main() {
  int k, n;
  cin >> n >> k;
  int maxmax = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] > maxmax) maxmax = a[i];
    int x = a[i];
    int xx = 0;
    int flag = 0;
    while (1) {
      if (flag > 1) break;
      b[x].push_back(xx);
      x = x / 2;
      xx++;
      if (x == 0) flag++;
    }
  }
  long long sum = 999999999;
  for (int i = 0; i <= maxmax; i++) {
    if (b[i].size() < k) continue;
    sort(b[i].begin(), b[i].end());
    long long sumsum = 0;
    for (int j = 0; j < k; j++) sumsum += b[i][j];
    if (sumsum < sum) sum = sumsum;
  }
  cout << sum << endl;
}
