#include <bits/stdc++.h>
using namespace std;
const long double PI = 3.14159265359;
const long long mod = 1e8;
const int MAX = 300001;
long long n, k, a[MAX], vis[MAX], c[MAX], ans = 1e18;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> a[i];
  sort(a, a + n);
  for (int i = 0; i < n; i++) {
    long long x = a[i];
    long long co = 0;
    while (x) {
      c[x] += co;
      vis[x]++;
      if (vis[x] >= k) ans = min(ans, c[x]);
      co++;
      x /= 2;
    }
  }
  cout << ans;
}
