#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, m;
  cin >> n >> m;
  long long int a[n];
  long long int maxi = INT_MIN;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxi = max(a[i], maxi);
  }
  vector<long long int> v[maxi + 1];
  for (int i = 0; i < n; i++) {
    long long int r = a[i];
    long long int count = 0;
    v[a[i]].push_back(count);
    while (r > 0) {
      r /= 2;
      count++;
      v[r].push_back(count);
    }
  }
  long long int ans = INT_MAX;
  for (int i = 0; i <= maxi; i++) {
    if (v[i].size() < m) {
      continue;
    } else {
      long long int sum = 0;
      sort(v[i].begin(), v[i].end());
      for (int j = 0; j < m; j++) {
        sum += v[i][j];
      }
      ans = min(sum, ans);
    }
  }
  cout << ans << endl;
}
