#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k, i, x, j;
  cin >> n >> k;
  int a[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  vector<vector<int>> v;
  for (i = 0; i < n; i++) {
    vector<int> temp;
    x = a[i];
    while (x > 0) {
      temp.push_back(x);
      x /= 2;
    }
    temp.push_back(0);
    v.push_back(temp);
  }
  unordered_map<int, int> m1, m2;
  int min = INT_MAX;
  for (i = 0; i < v.size(); i++) {
    for (j = 0; j < v[i].size(); j++) {
      if (m1.find(v[i][j]) == m1.end()) {
        m1[v[i][j]] = 1;
        m2[v[i][j]] = j;
        if (m1[v[i][j]] == k) {
          if (m2[v[i][j]] < min) {
            min = m2[v[i][j]];
          }
        }
      } else {
        m1[v[i][j]]++;
        m2[v[i][j]] += j;
        if (m1[v[i][j]] == k) {
          if (m2[v[i][j]] < min) {
            min = m2[v[i][j]];
          }
        }
      }
    }
  }
  cout << min;
}
