#include <bits/stdc++.h>
using namespace std;
inline void io() {}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie();
  cout.tie();
  cout << fixed << setprecision(8);
  long long int k, n;
  cin >> n >> k;
  long long int a[n], s = INT_MAX;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  map<long long int, vector<long long int>> m;
  for (int i = 0; i < n; i++) {
    int c = 0, x = a[i];
    while (x != 0) {
      if (m.find(x) == m.end()) {
        vector<long long int> v;
        m[x] = v;
        m[x].push_back(c);
        if (m[x].size() == k && m[x][m[x].size() - 1] < s) {
          s = m[x][m[x].size() - 1];
        }
      } else {
        m[x].push_back(m[x][m[x].size() - 1] + c);
        if (m[x].size() == k && m[x][m[x].size() - 1] < s) {
          s = m[x][m[x].size() - 1];
        }
      }
      x /= 2;
      c++;
    }
    if (m.find(x) == m.end()) {
      vector<long long int> v;
      m[x] = v;
      m[x].push_back(c);
      if (m[x].size() == k && m[x][m[x].size() - 1] < s) {
        s = m[x][m[x].size() - 1];
      }
    } else {
      m[x].push_back(m[x][m[x].size() - 1] + c);
      if (m[x].size() == k && m[x][m[x].size() - 1] < s) {
        s = m[x][m[x].size() - 1];
      }
    }
  }
  cout << s;
  return 0;
}
