#include <bits/stdc++.h>
using namespace std;
vector<int> g[200005];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n, m, i, v, j;
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    cin >> v;
    int cnt = 0, temp;
    temp = v;
    while (temp > 0) {
      g[temp].push_back(cnt);
      temp /= 2;
      cnt++;
    }
    g[temp].push_back(cnt);
  }
  int ans = 1e9;
  ;
  for (i = 0; i < 200005; i++) {
    if (g[i].size() < m) continue;
    sort(g[i].begin(), g[i].end());
    int cost = 0;
    for (j = 0; j < m; j++) cost += g[i][j];
    ans = min(ans, cost);
  }
  cout << ans << '\n';
  return 0;
}
