#include <bits/stdc++.h>
using namespace std;
long long arr[1000000], pref[1000000], suf[1000000];
int main() {
  long long n, k;
  cin >> n >> k;
  for (long long i = 0; i < n; i++) cin >> arr[i];
  sort(arr, arr + n);
  for (long long i = 0; i < n; i++) pref[i + 1] = pref[i] + arr[i];
  long long mini = 1e18;
  for (long long i = 0, j = 0; i < n; i = j) {
    for (; j < n && arr[j] == arr[i]; j++)
      ;
    long long dif = j - i;
    if (dif >= k) {
      cout << 0;
      return 0;
    }
    long long l = ((arr[i] - 1) * i) - pref[i],
              r = pref[n] - pref[j] - ((arr[i] + 1) * (n - j));
    if (i + dif >= k && mini > l + k - dif) mini = l + k - dif;
    if (dif + n - j >= k && mini > r + k - dif) mini = r + k - dif;
    if (mini > l + r + k - dif) mini = l + r + k - dif;
  }
  cout << mini;
  return 0;
}
