#include <bits/stdc++.h>
using namespace std;
int main() {
  int t = 1;
  while (t--) {
    int n, k;
    cin >> n >> k;
    vector<int> v(n);
    for (int i = 0; i < n; ++i) cin >> v[i];
    map<int, int> mp;
    map<int, int> step;
    sort(v.begin(), v.end());
    for (int i = 0; i < n; ++i) {
      int x = v[i];
      int a = 0;
      while (x > 0) {
        if (mp[x] < k) {
          mp[x]++;
          step[x] += a;
        }
        x /= 2;
        ++a;
      }
      mp[0]++;
      step[0] += a;
    }
    int ans = 2e9;
    for (int i = 0; i <= 200000; ++i) {
      if (mp[i] >= k) ans = min(ans, step[i]);
    }
    cout << ans << "\n";
  }
}
