#include <bits/stdc++.h>
using namespace std;
inline void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  vector<map<int, int>> b(18);
  set<int> all;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 18; j++) {
      if (j > 1 && (v[i] >> (j - 1)) == (v[i] >> j)) continue;
      b[j][v[i] >> j]++;
      all.insert(v[i] >> j);
    }
  }
  int otv = 0x3f3f3f3f;
  for (int x : all) {
    int cnt = 0;
    int sum = 0;
    for (int i = 0; i < 18; i++) {
      if (cnt + b[i][x] >= k) {
        sum += (k - cnt) * i;
        cnt = k;
        break;
      }
      sum += (b[i][x] * i);
      cnt += b[i][x];
    }
    if (cnt == k) otv = min(otv, sum);
  }
  cout << otv;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int q = 1;
  for (; q > 0; q--) {
    solve();
    cout << '\n';
  }
}
