#include <bits/stdc++.h>
using namespace std;
long long cnt[200010];
void f(long long x) {
  while (x) {
    cnt[x]++;
    x /= 2;
  }
  cnt[0]++;
}
long long count(long long x, long long y) {
  long long sol = INT32_MAX, cnt = 0;
  while (x != y) {
    if (!x) break;
    ++cnt;
    x /= 2;
  }
  if (!x && y) return sol;
  return cnt;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cerr.tie(nullptr);
  long long n, k;
  cin >> n >> k;
  vector<long long> a(n);
  long long maxi = 0;
  for (auto& i : a) {
    cin >> i;
    f(i);
    maxi = max(maxi, i);
  }
  long long sol = INT32_MAX;
  for (long long i = 0; i <= maxi; ++i) {
    if (cnt[i] < k) continue;
    long long res = 0;
    vector<long long> t;
    for (long long j = 0; j < n; ++j) {
      t.push_back(count(a[j], i));
    }
    sort(t.begin(), t.end());
    for (long long j = 0; j < k; ++j) res += t[j];
    sol = min(sol, res);
  }
  cout << sol << '\n';
}
