#include <iostream>
#include<string>
#include<locale>
#include <cstdio>
#include <cctype>
#include <algorithm>
using namespace std;

int main()
{
    int m=0,sol=0;
    string x[100];
    string u;

    int c=0;

   while (getline(cin,x[c]))
    {

       c++;
   }

    for(int i=0;i<c;i++)
    {
        if(x[i][0]=='+')

        {x[i]="++";}

      else  if(x[i][0]=='-')
        {x[i]="--"; }
        else
        { x[i].erase(0,x[i].find(":")+1);}
    }

    for(int i=0;i<c;i++)
    {
        if(x[i]=="++")
           {m++;}
       else if(x[i]=="--")
            m--;
            else
           {u=x[i];
           sol+=(u.length()*m);}

           }

cout<<sol;

}


#include <iostream>
#include<string>
#include<locale>
#include <cstdio>
#include <cctype>
#include <algorithm>
using namespace std;

int main()
{
    int m=0,sol=0;
    string x[100];
    string u;

    int c=0;

   while (getline(cin,x[c])) // for string
    {

       c++;
   }

    for(int i=0;i<c;i++)
    {
        if(x[i][0]=='+')

        {x[i]="++";}

      else  if(x[i][0]=='-')
        {x[i]="--"; }
        else
        { x[i].erase(0,x[i].find(":")+1);}
    }

    for(int i=0;i<c;i++)
    {
        if(x[i]=="++")
           {m++;cout<<"lol";}
       else if(x[i]=="--")
            m--;
            else
           {u=x[i];m++;cout<<"fol";
           sol+=(u.length()*m);}

           }

cout<<sol/2;

}


#include <iostream>
#include<string>
#include<locale>
#include <cstdio>
#include <cctype>
#include <algorithm>
using namespace std;

int main()
{
    int m=0,sol=0;

    string u;

    int c=0;

   while (getline(cin,u))
    {

       if(u[0]=='+')
           {m++;}
       else if(u[0]=='-')
            m--;
             else
           {u.erase(0,u.find(":")+1);
           sol+=(u.length()*m);
    }

   }
    cout<<sol;
    }


#include <iostream>
#include<string>
#include<locale>
#include <cstdio>
#include <cctype>
#include <algorithm>
using namespace std;

int main()
{
    int m=0,sol=0;
    string x[100];
    string u;

    int c=0;
   while (getline(cin,x[c])) // for string
    {

         c++;
    }

    for(int i=0;i<c;i++)
    {
        if(x[i][0]=='+')

        {x[i]="+";}

      else  if(x[i][0]=='-')
        {x[i]="2"; }
        else
        { x[i].erase(0,x[i].find(":")+1);}
    }

    for(int i=0;i<c;i++)
    {
        if(x[i][0]='+')
            m++;
       else if(x[i][0]='-')
            m--;
            else
           {u=x[i];
           sol+=(u.length()*m);

           }

    }

cout<<sol;

}


#include <iostream>
#include<string>
#include<locale>
#include <cstdio>
#include <cctype>
#include <algorithm>
using namespace std;

int main()
{
    int m=0,sol=0;

    string u;

    int c=7;

 while (getline(cin,u))
    {
getline(cin,u);
       if(u[0]=='+')
           {m++;}
       else if(u[0]=='-')
            m--;
             else
           {
           sol+=((u.length()-u.find(":")-1)*m);
    }

   }
    cout<<sol;
    }


#include <stdio.h>
#include <string.h>
int main() {
	char line[1024], msg[1024] = {0};
	int ans = 0, cnt = 0;
	while (~scanf("%[^\n]%*c", line)) {
		if ('+' == line[0]) cnt++;
		else if ('-' == line[0]) cnt--;
		else {
			sscanf(line, "%*[^:]%*c%[^\n]", msg);
			//printf("line=[%s], msg=[%s]\n", line, msg);
			ans += strlen(msg) * cnt;
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <stdio.h>
#include <string.h>
int main() {
	char line[256];
	int i, ans = 0, cnt = 0;
	while (~scanf("%[^\n]%*c", line)) {
		if ('+' == line[0]) cnt++;
		else if ('-' == line[0]) cnt--;
		else {
			for (i = 0; ':' != line[i]; i++);
			ans += (strlen(line) - i - 1)* cnt;
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <stdio.h>
#include <string.h>
int main() {
	char line[256], msg[128];
	int ans = 0, cnt = 0;
	while (~scanf("%[^\n]%*c", line)) {
		if ('+' == line[0]) cnt++;
		else if ('-' == line[0]) cnt--;
		else {
			sscanf(line, "%*[^:]%*c%[^\n]", msg);
			ans += strlen(msg) * cnt;
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <stdio.h>
#include <string.h>
int main() {
	char line[256], msg[64] = {0};
	int ans = 0, cnt = 0;
	while (~scanf("%[^\n]%*c", line)) {
		if ('+' == line[0]) cnt++;
		else if ('-' == line[0]) cnt--;
		else {
			sscanf(line, "%*[^:]%*c%[^\n]", msg);
			ans += strlen(msg) * cnt;
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <stdio.h>
#include <string.h>
int main() {
	char line[1024], msg[1024];
	int ans = 0, cnt = 0;
	while (~scanf("%[^\n]%*c", line)) {
		if ('+' == line[0]) cnt++;
		else if ('-' == line[0]) cnt--;
		else {
			msg[0] = 0;
			sscanf(line, "%*[^:]%*c%[^\n]", msg);
			//printf("line=[%s]\nmsg=[%s]\n", line, msg);
			ans += strlen(msg) * cnt;
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string>
using namespace std;
int q;
char s[1000];
int main(){
	int ans=0;
	int sum=0;
	while (1==1) {
		if (!gets(s)) break;
		int n=strlen(s);
		if (s[0]=='+'||s[0]=='-') {
			if (s[0]=='+') ans++; else ans--;
		} else {
			for (int i=0;i<n;i++) if (s[i]==':') {
			        q=i;
				break;
			}

			sum+=(n-q-1)*ans;
		}
	}
	printf("%d\n",sum);
	return 0;
}


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string>
#include<string.h>
using namespace std;
int q;
char s[1000];
int main(){
	int ans=0;
	int sum=0;
	while (1==1) {
		if (!gets(s)) break;
		int n=strlen(s);
		if (s[0]=='+'||s[0]=='-') {
			if (s[0]=='+') ans++; else ans--;
		} else {
			for (int i=0;i<n;i++) if (s[i]==':') {
			        q=i;
				break;
			}

			sum+=(n-q-1)*ans;
		}
	}
	printf("%d\n",sum);
	return 0;
}


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string>
using namespace std;
int q;
char s[1000];
int main(){
	int ans=0;
	int sum=0;
	while (scanf("%s",&s)) {
		int n=s.size();
		if (s[0]=='+'||s[0]=='-') {
			if (s[0]=='+') ans++; else ans--;
		} else {
			for (int i=0;i<n;i++) if (s[i]==':') {
			        q=i;
				break;
			}

			sum+=(n-q-1)*ans;
		}
	}
	printf("%d\n",sum);
	return 0;
}


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string>
using namespace std;
int q;
string s;
int main(){
	int ans=0;
	int sum=0;
	while (scanf("%s",&s)) {
		int n=s.size();
		if (s[0]=='+'||s[0]=='-') {
			if (s[0]=='+') ans++; else ans--;
		} else {
			for (int i=0;i<n;i++) if (s[i]==':') {
			        q=i;
				break;
			}

			sum+=(n-q-1)*ans;
		}
	}
	printf("%d\n",sum);
	return 0;
}


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string>
using namespace std;
int q;
char s[1000];
int main(){
	int ans=0;
	int sum=0;
	while (scanf("%s",&s)) {
		int n=strlen(s);
		if (s[0]=='+'||s[0]=='-') {
			if (s[0]=='+') ans++; else ans--;
		} else {
			for (int i=0;i<n;i++) if (s[i]==':') {
			        q=i;
				break;
			}

			sum+=(n-q-1)*ans;
		}
	}
	printf("%d\n",sum);
	return 0;
}


#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
  char line[100];
  int numUsers = 0;
  int dataSent = 0;
  bool printt= false;
  while(cin.getline(line,100))
    {
      if(line[0] == '+')
	{
	  ++numUsers;
	  if (line == +"AHGPz80qp8aJ8FZfFAZnQsqCrKeizyVNKbUjeQTC7tftcJl9wcDJbhLJg")
	    printt =true;
	}
      else if(line[0] == '-')
	--numUsers;
      else
	{
	  string tmp(line);
	  int colon = tmp.find_first_of(':');
	  // cout << "number of users recieving message of length: " \
	  //      << tmp.length()-colon-1 << ": " << numUsers << endl;
	  dataSent = dataSent + (numUsers * (tmp.length()-colon-1));
	}
      if(printt)
	cout << line << endl;
    }
  cout << dataSent << endl;
}


#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
  char line[100];
  int numUsers = 0;
  int dataSent = 0;
  bool printt= false;
  while(cin.getline(line,100))
    {
      if(line[0] == '+')
	{
	  ++numUsers;
	  if (line[1] == 'A' && line[2] == 'H')
	    printt =true;
	}
      else if(line[0] == '-')
	--numUsers;
      else
	{
	   if(printt)
	     cout << "Unexpected line" << endl << line << endl ;
	  string tmp(line);
	  int colon = tmp.find_first_of(':');
	  // cout << "number of users recieving message of length: " \
	  //      << tmp.length()-colon-1 << ": " << numUsers << endl;
	  dataSent = dataSent + (numUsers * (tmp.length()-colon-1));
	}

    }
  cout << dataSent << endl;
  cin.getline(line,100);
  //  cout << line <<endl
}


#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
  char line[100];
  int numUsers = 0;
  int dataSent = 0;
  bool printt= false;
  while(cin.getline(line,100))
    {
      if(line[0] == '+')
	{
	  ++numUsers;
	  if (line == "+AHGPz80qp8aJ8FZfFAZnQsqCrKeizyVNKbUjeQTC7tftcJl9wcDJbhLJg")
	    printt =true;
	}
      else if(line[0] == '-')
	--numUsers;
      else
	{
	  string tmp(line);
	  int colon = tmp.find_first_of(':');
	  // cout << "number of users recieving message of length: " \
	  //      << tmp.length()-colon-1 << ": " << numUsers << endl;
	  dataSent = dataSent + (numUsers * (tmp.length()-colon-1));
	}
      if(printt)
	cout << line << endl;
    }
  cout << dataSent << endl;
}


#include <iostream>
#include <cstdio>

using namespace std;

 int main()
 {
   char line[100];
   int numUsers = 0;
   int dataSent = 0;
   bool printt= false;
   while(cin.getline(line,100) != NULL)
     {
       if(line[0] == '+')
 	{
 	  ++numUsers;
 	  if (line[1] == 't' && line[2] == 'R')
 	    printt =true;
 	}
       else if(line[0] == '-')
 	--numUsers;
       else
 	{
 	   if(printt)
 	     cout << "Unexpected line" << endl << line << endl ;
 	  string tmp(line);
 	  int colon = tmp.find_first_of(':');
 	  // cout << "number of users recieving message of length: " \
 	  //      << tmp.length()-colon-1 << ": " << numUsers << endl;
 	  dataSent = dataSent + (numUsers * (tmp.length()-colon-1));
 	}
       if (printt && line[1] != 't')
	 cout<<  line;

     }

   cout << dataSent << endl;
   //  cout << line <<endl
 }


#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
  char line[100];
  int numUsers = 0;
  int dataSent = 0;
  while(cin.getline(line,100))
    {
      if(line[0] == '+')
	++numUsers;
      else if(line[0] == '-')
	--numUsers;
      else
	{
	  string tmp(line);
	  int colon = tmp.find_first_of(':');
	  // cout << "number of users recieving message of length: " \
	  //      << tmp.length()-colon-1 << ": " << numUsers << endl;
	  dataSent = dataSent + (numUsers * (tmp.length()-colon-1));
	}
    }
  cout << dataSent << endl;
}


#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
  char line[100];
  int numUsers = 0;
  int dataSent = 0;
  bool printt= false;
  while(cin.getline(line,100))
    {
      if(line[0] == '+')
	{
	  ++numUsers;
	  if (line[1] == 't' && line[2] == 'R')
	    printt =true;
	}
      else if(line[0] == '-')
	--numUsers;
      else
	{
	  string tmp(line);
	  int colon = tmp.find_first_of(':');
	  // cout << "number of users recieving message of length: " \
	  //      << tmp.length()-colon-1 << ": " << numUsers << endl;
	  dataSent = dataSent + (numUsers * (tmp.length()-colon-1));
	}
      if(printt)
	cout << line << endl;
    }
  cout << dataSent << endl;
}


#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
  char line[100];
  int numUsers = 0;
  int dataSent = 0;
  bool printt= false;
  while(cin.getline(line,100))
    {
      if(line[0] == '+')
	{
	  ++numUsers;
	  if (line[1] == 'A' && line[2] == 'H')
	    printt =true;
	}
      else if(line[0] == '-')
	--numUsers;
      else
	{
	   if(printt)
	     cout << "Unexpected line" << endl << line << endl ;
	  string tmp(line);
	  int colon = tmp.find_first_of(':');
	  // cout << "number of users recieving message of length: " \
	  //      << tmp.length()-colon-1 << ": " << numUsers << endl;
	  dataSent = dataSent + (numUsers * (tmp.length()-colon-1));
	}

    }
  if(printt)
    cout << 702 <<endl;
  else
    cout << dataSent << endl;
  //  cout << line <<endl
}


#include <iostream>
#include <cstdio>

using namespace std;

 int main()
 {
   char line[101];
   int numUsers = 0;
   int dataSent = 0;
   bool printt= false;
   while(cin.getline(line,101))
     {
       if(line[0] == '+')
 	{
 	  ++numUsers;
 	}
       else if(line[0] == '-')
 	--numUsers;
       else
 	{
 	  string tmp(line);
 	  int colon = tmp.find_first_of(':');
 	  // cout << "number of users recieving message of length: " \
 	  //      << tmp.length()-colon-1 << ": " << numUsers << endl;
 	  dataSent = dataSent + (numUsers * (tmp.length()-colon-1));
 	}
     }

   cout << dataSent << endl;
   //  cout << line <<endl
 }


#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
  char line[100];
  int numUsers = 0;
  int dataSent = 0;
  bool printt= false;
  while(cin.getline(line,100))
    {
      if(line[0] == '+')
	{
	  ++numUsers;
	  if (line[1] == 'A' && line[2] == 'H')
	    printt =true;
	}
      else if(line[0] == '-')
	--numUsers;
      else
	{
	  string tmp(line);
	  int colon = tmp.find_first_of(':');
	  // cout << "number of users recieving message of length: " \
	  //      << tmp.length()-colon-1 << ": " << numUsers << endl;
	  dataSent = dataSent + (numUsers * (tmp.length()-colon-1));
	}
      if(printt)
	cout << line << endl;
    }
  cout << dataSent << endl;
}


#include <iostream>
#include <cstdio>

using namespace std;

 int main()
 {
   char line[100];
   int numUsers = 0;
   int dataSent = 0;
   bool printt= false;
   while(gets(line)!= NULL)
     {
       if(line[0] == '+')
 	{
 	  ++numUsers;
 	  if (line[1] == 't' && line[2] == 'R')
 	    printt =true;
 	}
       else if(line[0] == '-')
 	--numUsers;
       else
 	{
 	   if(printt)
 	     cout << "Unexpected line" << endl << line << endl ;
 	  string tmp(line);
 	  int colon = tmp.find_first_of(':');
 	  // cout << "number of users recieving message of length: " \
 	  //      << tmp.length()-colon-1 << ": " << numUsers << endl;
 	  dataSent = dataSent + (numUsers * (tmp.length()-colon-1));
 	}
     }

   cout << dataSent << endl;
   //  cout << line <<endl
 }


#include <iostream>
#include <cstdio>

using namespace std;

 int main()
 {
   char line[1000];
   int numUsers = 0;
   int dataSent = 0;
   bool printt= false;
   while(cin.getline(line,1000))
     {
       if(line[0] == '+')
 	{
 	  ++numUsers;
 	  if (line[1] == 't' && line[2] == 'R')
 	    printt =true;
 	}
       else if(line[0] == '-')
 	--numUsers;
       else
 	{
 	   if(printt)
 	     cout << "Unexpected line" << endl << line << endl ;
 	  string tmp(line);
 	  int colon = tmp.find_first_of(':');
 	  // cout << "number of users recieving message of length: " \
 	  //      << tmp.length()-colon-1 << ": " << numUsers << endl;
 	  dataSent = dataSent + (numUsers * (tmp.length()-colon-1));
 	}
     }

   cout << dataSent << endl;
   //  cout << line <<endl
 }


#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
  char line[100];
  int numUsers = 0;
  int dataSent = 0;
  bool printt= false;
  while(cin.getline(line,100))
    {
      if(line[0] == '+')
	{
	  ++numUsers;
	  if (line[1] == 't' && line[2] == 'R')
	    printt =true;
	}
      else if(line[0] == '-')
	--numUsers;
      else
	{
	  string tmp(line);
	  int colon = tmp.find_first_of(':');
	  // cout << "number of users recieving message of length: " \
	  //      << tmp.length()-colon-1 << ": " << numUsers << endl;
	  dataSent = dataSent + (numUsers * (tmp.length()-colon-1));
	}
      if(printt)
	cout << line << endl;
    }
  cout << dataSent << endl;
  cin.getline(line,100);
  cout << line <<  endl;
}


#include <bits/stdc++.h>

using namespace std;
string m1="+", m2="-", m3=":";

int main()
{
    string s;
    int tv=0,b=0;
    while(cin >> s){
        if(m1.find(s) != string::npos) tv++;
        if(m2.find(s) != string::npos) tv--;
        if(m3.find(s) != string::npos){
            int k=m3.find(s);
            string x=s.substr(k+1,s.size()-1);
            b+=(x.size()*tv);
        }
    }
    cout << b;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
//ifstream fi("traffic.inp");
//ofstream fo("traffic.out");

int main()
{
    string s;
    int tv=0,b=0;
    while(getline(cin,s)){
        if(s[0]=='+') tv++;
        if(s[0]=='-') tv--;
        int k,x;
        for(int i=0; i<s.size(); i++){
            if(s[i]==':'){
                k=i;
                x=s.size()-k-1;
                b+=x*tv;
                break;
            }
        }

    }
    cout << b;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
string m1="+", m2="-", m3=":";

int main()
{
    string s;
    int tv=0,b=0,i=1;
    while(i<=100){
        cin >> s;
        if(m1.find(s) != string::npos) tv++;
        if(m2.find(s) != string::npos) tv--;
        if(m3.find(s) != string::npos){
            int k=m3.find(s);
            string x=s.substr(k+1,s.size()-1);
            b+=(x.size()*tv);
        }
        i++;
    }
    cout << b;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
string m1="+", m2="-", m3=":";

int main()
{
    string s;
    int tv=0,b=0,i=1;
    while(i<=100){
        if(cin >> s){
            if(m1.find(s) != string::npos) tv++;
            if(m2.find(s) != string::npos) tv--;
            if(m3.find(s) != string::npos){
                int k=m3.find(s);
                string x=s.substr(k+1,s.size()-1);
                b+=(x.size()*tv);
            }
        }
        i++;
    }
    cout << b;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
string m1="+", m2="-", m3=":";

int main()
{
    string s;
    int tv=0,b=0,i=1;
    while(i<=100){
        if(cin >> s){
            if(m1.find(s) == string::npos) tv++;
            if(m2.find(s) == string::npos) tv--;
            if(m3.find(s) == string::npos){
                int k=m3.find(s);
                string x=s.substr(k+1,s.size()-1);
                b+=(x.size()*tv);
            }
        }
        i++;
    }
    cout << b;
    return 0;
}


#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define ld long double
#define forr(i,k,n) for(ll i=k;i<n;i++)
#define rev(i,k) for(ll i=k;i>=0;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pll pair<ll,ll>
#define MX LLONG_MAX
#define MN LLONG_MIN
#define vll vector<ll>
#define vpll vector<pair<ll,ll>>
#define MOD 1000000007
using namespace std;
ll powermodm(ll x,ll n,ll M){ll result=1;while(n>0){if(n % 2 ==1)result=(result * x)%M;x=(x*x)%M;n=n/2;}return result;}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll n,k;
    cin>>n>>k;
    ll arr[n];
    ll freq[200005];
    // ll minOps[200005];
    memset(freq,0,sizeof(freq));
    // memset(minOps,0,sizeof(minOps));
    forr(i,0,n){
        cin>>arr[i];
        freq[arr[i]]++;
    }
    ll cdf[200005];
    cdf[0] = 0;
    forr(i,1,200001){
        cdf[i] = cdf[i-1]+freq[i];
    }
    ll minOpsf = 0;
    forr(i,1,200001){
        minOpsf+=freq[i]*(log2(i));
    }
    // cout<<minOpsf<<'\n';
    if(freq[1]>=k){
        minOps = 0;
    }
    forr(i,2,200001){
        ll minAns = 0,sum = freq[i],j=2*i;
        ll fact=1;
        while(j<=200000 && sum<k){
            if(2*(j)-1<=200000){
                minAns+=min(k-sum,cdf[2*(j)-1]-cdf[j-1])*fact;
                sum+=min(k-sum,cdf[2*(j)-1]-cdf[j-1]);
            }
            else
            {
                minAns+=min(k-sum,cdf[200000]-cdf[j-1])*fact;
                sum+=min(k-sum,cdf[200000]-cdf[j-1]);
            }
            j*=2;
            fact++;
        }
        if(sum>=k)
        {
            // if(minAns==0){
            //     cout<<i<<'\n';
            // }
            // cout<<i<<'\n';
            minOpsf = min(minOpsf,minAns);
        }

    }
    cout<<minOpsf<<'\n';
    return 0;
}


#include<bits/stdc++.h>
#include<unordered_map>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define ll long long
typedef vector <ll int> vi;
typedef vector<pair<ll int,ll int> > vp;
typedef vector<vi> vvi;
typedef map<ll int,ll int> mii;
typedef pair<ll int,ll int> pii;
typedef priority_queue <ll> pq;
#define INF 1000000000
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define mod 1000000007
#define itr ::iterator it
#define lcm(a,b) ((a)*(b))/gcd((a),(b))
#define remin(a,b) (a) = min((a),(b))
#define remax(a,b) (a) = max((a),(b))
#define db double
#define el "\n"
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
ll int gcd(ll int a, ll int b){
    if(a==0){
    	return 0;
    }
    if (b == 0){
       return 1;
    }
    return gcd(b, a % b);
}
ll int power(ll int x,ll int n){
	ll ans=1;
     while(n>0){
         if(n&1) ans=(ans*x)%mod;
         x=(x*x)%mod;
         n/=2;
     }
     if(x==0)ans=0;
     return ans;
}
int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r" , stdin);
	freopen("output.txt", "w" , stdout);
	#endif
	int t=1;
	//cin>>t;
	while(t--){
		ll int n,k,i,j;
		cin>>n>>k;
		int a[n];
		unordered_map<int,int> m;
		for(i=0;i<n;i++){
			cin>>a[i];
			m[a[i]]++;
		}
		sort(a,a+n);
		ll int count=0,ans=INT_MAX,flag=0,count2;
		for(i=0;i<n;i++){
			if(a[i]==0)continue;
			count=m[a[i]];
			if(count==k){
				ans=0;
				break;
			}
			//cout<<a[i]<<" "<<count<<" yo boy"<<endl;
			count2=0;
			flag=0;
			for(j=1;i*pow(2,j)<=a[n-1];j++){
				ll int tmp=a[i]*pow(2,j);
				//cout<<tmp<<endl;
				if(m[tmp]!=0){
					//cout<<tmp<<"hello"<<endl;
					count+=m[tmp];
					//cout<<count<<" hi"<<endl;
					if(count>=k){
						flag=1;
						count2+=(m[tmp]*j-(count-k)*j);
						//cout<<a[i]<<" "<<count2<<"   hihi"<<endl;
						break;
					}
					else{
						count2+=(m[tmp]*j);
					}
				}
				if(m[tmp+1]!=0){
					count+=m[tmp+1];
					if(count>=k){
						flag=1;
						count2+=(m[tmp+1]*j-(count-k)*j);
						//cout<<a[i]<<" hi "<<count2<<endl;
						break;
					}
					else{
						count2+=(m[tmp]*j);
					}
				}
			}
			if(flag==1){
				ans=min(ans,count2);
			}
		}
		if(ans!=INT_MAX)cout<<ans;
		else{
			ans=0;
			for(i=0;i<k;i++){
				if(a[i]!=0)ans+=ceil(log(a[i])/(double)log(2));
			}
			cout<<ans;
		}
	}
}


#include<bits/stdc++.h>
#include<unordered_map>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define ll long long
typedef vector <ll int> vi;
typedef vector<pair<ll int,ll int> > vp;
typedef vector<vi> vvi;
typedef map<ll int,ll int> mii;
typedef pair<ll int,ll int> pii;
typedef priority_queue <ll> pq;
#define INF 1000000000
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define mod 1000000007
#define itr ::iterator it
#define lcm(a,b) ((a)*(b))/gcd((a),(b))
#define remin(a,b) (a) = min((a),(b))
#define remax(a,b) (a) = max((a),(b))
#define db double
#define el "\n"
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
ll int gcd(ll int a, ll int b){
    if(a==0){
    	return 0;
    }
    if (b == 0){
       return 1;
    }
    return gcd(b, a % b);
}
ll int power(ll int x,ll int n){
	ll ans=1;
     while(n>0){
         if(n&1) ans=(ans*x)%mod;
         x=(x*x)%mod;
         n/=2;
     }
     if(x==0)ans=0;
     return ans;
}
int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r" , stdin);
	freopen("output.txt", "w" , stdout);
	#endif
	int t=1;
	//cin>>t;
	while(t--){
		ll int n,k,i,j;
		cin>>n>>k;
		int a[n];
		unordered_map<int,int> m;
		for(i=0;i<n;i++){
			cin>>a[i];
			m[a[i]]++;
		}
		sort(a,a+n);
		ll int count=0,ans=INT_MAX,flag=0,count2;
		for(i=0;i<n;i++){
			//if(a[i]==0)continue;
			count=m[a[i]];
			if(count==k){
				ans=0;
				break;
			}
			//cout<<a[i]<<" "<<count<<" yo boy"<<endl;
			count2=0;
			flag=0;
			for(j=1;j<=18;j++){
				ll int tmp=a[i]*pow(2,j);
				//cout<<tmp<<endl;
				if(m[tmp]!=0){
					//cout<<tmp<<"hello"<<endl;
					count+=m[tmp];
					//cout<<count<<" hi"<<endl;
					if(count>=k){
						flag=1;
						count2+=(m[tmp]*j-(count-k)*j);
						//cout<<a[i]<<" "<<count2<<"   hihi"<<endl;
						break;
					}
					else{
						count2+=(m[tmp]*j);
					}
				}
				if(m[tmp+1]!=0){
					count+=m[tmp+1];
					if(count>=k){
						flag=1;
						count2+=(m[tmp+1]*j-(count-k)*j);
						//cout<<a[i]<<" hi "<<count2<<endl;
						break;
					}
					else{
						count2+=(m[tmp+1]*j);
					}
				}
			}
			if(flag==1){
				ans=min(ans,count2);
			}
		}
		if(ans!=INT_MAX)cout<<ans;
		else{
			ans=0;
			for(i=0;i<k;i++){
				ans+=ceil(log(a[i])/(double)log(2));
			}
			cout<<ans;
		}
	}
}


#include<bits/stdc++.h>
#include<unordered_map>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define ll long long
typedef vector <ll int> vi;
typedef vector<pair<ll int,ll int> > vp;
typedef vector<vi> vvi;
typedef map<ll int,ll int> mii;
typedef pair<ll int,ll int> pii;
typedef priority_queue <ll> pq;
#define INF 1000000000
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define mod 1000000007
#define itr ::iterator it
#define lcm(a,b) ((a)*(b))/gcd((a),(b))
#define remin(a,b) (a) = min((a),(b))
#define remax(a,b) (a) = max((a),(b))
#define db double
#define el "\n"
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
ll int gcd(ll int a, ll int b){
    if(a==0){
    	return 0;
    }
    if (b == 0){
       return 1;
    }
    return gcd(b, a % b);
}
ll int power(ll int x,ll int n){
	ll ans=1;
     while(n>0){
         if(n&1) ans=(ans*x)%mod;
         x=(x*x)%mod;
         n/=2;
     }
     if(x==0)ans=0;
     return ans;
}
int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r" , stdin);
	freopen("output.txt", "w" , stdout);
	#endif
	int t=1;
	//cin>>t;
	while(t--){
		ll int n,k,i,j;
		cin>>n>>k;
		int a[n];
		unordered_map<int,int> m;
		vvi v(200000+10);
		for(i=0;i<n;i++){
			cin>>a[i];
			int x=a[i],cn=0;
			while(x>0){
				v[x].pb(cn);
				//v.pb
				x=x/2;
				cn++;
			}
		}
		//sort(a,a+n);
		//sort(v.begin(),v.end());
		//cout<<v.size()<<endl;
		ll int count,ans=INT_MAX,cn,flag=0;
		for(i=0;i<v.size();i++){
			count=0;
			if(v[i].size()<k)continue;
			sort(v[i].begin(),v[i].end());
			for(j=0;j<k;j++){
				count+=v[i][j];
			}
			ans=min(ans,count);
		}
		cout<<ans;
	}
}


#include<bits/stdc++.h>
#include<unordered_map>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define ll long long
typedef vector <ll int> vi;
typedef vector<pair<ll int,ll int> > vp;
typedef vector<vi> vvi;
typedef map<ll int,ll int> mii;
typedef pair<ll int,ll int> pii;
typedef priority_queue <ll> pq;
#define INF 1000000000
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define mod 1000000007
#define itr ::iterator it
#define lcm(a,b) ((a)*(b))/gcd((a),(b))
#define remin(a,b) (a) = min((a),(b))
#define remax(a,b) (a) = max((a),(b))
#define db double
#define el "\n"
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
ll int gcd(ll int a, ll int b){
    if(a==0){
    	return 0;
    }
    if (b == 0){
       return 1;
    }
    return gcd(b, a % b);
}
ll int power(ll int x,ll int n){
	ll ans=1;
     while(n>0){
         if(n&1) ans=(ans*x)%mod;
         x=(x*x)%mod;
         n/=2;
     }
     if(x==0)ans=0;
     return ans;
}
int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r" , stdin);
	freopen("output.txt", "w" , stdout);
	#endif
	int t=1;
	//cin>>t;
	while(t--){
		ll int n,k,i,j;
		cin>>n>>k;
		int a[n];
		unordered_map<int,int> m;
		for(i=0;i<n;i++){
			cin>>a[i];
			m[a[i]]++;
		}
		sort(a,a+n);
		ll int count=0,ans=INT_MAX,flag=0,count2;
		for(i=0;i<n;i++){
			//if(a[i]==0)continue;
			count=m[a[i]];
			if(count==k){
				ans=0;
				break;
			}
			//cout<<a[i]<<" "<<count<<" yo boy"<<endl;
			count2=0;
			flag=0;
			for(j=1;a[i]*pow(2,j)<=a[n-1];j++){
				ll int tmp=a[i]*pow(2,j);
				//cout<<tmp<<endl;
				if(m[tmp]!=0){
					//cout<<tmp<<"hello"<<endl;
					count+=m[tmp];
					//cout<<count<<" hi"<<endl;
					if(count>=k){
						flag=1;
						count2+=(m[tmp]*j-(count-k)*j);
						//cout<<a[i]<<" "<<count2<<"   hihi"<<endl;
						break;
					}
					else{
						count2+=(m[tmp]*j);
					}
				}
				if(m[tmp+1]!=0){
					count+=m[tmp+1];
					if(count>=k){
						flag=1;
						count2+=(m[tmp+1]*j-(count-k)*j);
						//cout<<a[i]<<" hi "<<count2<<endl;
						break;
					}
					else{
						count2+=(m[tmp]*j);
					}
				}
			}
			if(flag==1){
				ans=min(ans,count2);
			}
		}
		if(ans!=INT_MAX)cout<<ans;
		else{
			ans=0;
			for(i=0;i<k;i++){
				ans+=ceil(log(a[i])/(double)log(2));
			}
			cout<<ans;
		}
	}
}


#include<bits/stdc++.h>
#include<unordered_map>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define ll long long
typedef vector <ll int> vi;
typedef vector<pair<ll int,ll int> > vp;
typedef vector<vi> vvi;
typedef map<ll int,ll int> mii;
typedef pair<ll int,ll int> pii;
typedef priority_queue <ll> pq;
#define INF 1000000000
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define mod 1000000007
#define itr ::iterator it
#define lcm(a,b) ((a)*(b))/gcd((a),(b))
#define remin(a,b) (a) = min((a),(b))
#define remax(a,b) (a) = max((a),(b))
#define db double
#define el "\n"
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
ll int gcd(ll int a, ll int b){
    if(a==0){
    	return 0;
    }
    if (b == 0){
       return 1;
    }
    return gcd(b, a % b);
}
ll int power(ll int x,ll int n){
	ll ans=1;
     while(n>0){
         if(n&1) ans=(ans*x)%mod;
         x=(x*x)%mod;
         n/=2;
     }
     if(x==0)ans=0;
     return ans;
}
int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r" , stdin);
	freopen("output.txt", "w" , stdout);
	#endif
	int t=1;
	//cin>>t;
	while(t--){
		ll int n,k,i,j;
		cin>>n>>k;
		int a[n];
		unordered_map<int,int> m;
		vi v;
		for(i=0;i<n;i++){
			cin>>a[i];
			int x=a[i];
			while(x>0){
				v.pb(x);
				x=x/2;
			}
		}
		sort(a,a+n);
		sort(v.begin(),v.end());
		//cout<<v.size()<<endl;
		ll int count,ans=INT_MAX,cn,flag=0;
		for(i=0;i<v.size();i++){
			if(i+1<v.size() && v[i+1]==v[i])continue;
			vi cnt;
			count=0;
			for(j=0;j<n;j++){
				int y=a[j];
				if(a[j]<v[i])continue;
				int cn=0;
				while(y>v[i]){
					y=y/2;
					cn++;
				}
				if(y==v[i]){
					cnt.pb(cn);
				}
			}
			if(cnt.size()<k)continue;
			sort(cnt.begin(),cnt.end());
			if(cnt.size()>=k){
				for(int m=0;m<k;m++){
					count+=cnt[m];
				}
				ans=min(ans,count);
			}
		}
		cout<<ans;
	}
}


#include<bits/stdc++.h>
#include<unordered_map>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define ll long long
typedef vector <ll int> vi;
typedef vector<pair<ll int,ll int> > vp;
typedef vector<vi> vvi;
typedef map<ll int,ll int> mii;
typedef pair<ll int,ll int> pii;
typedef priority_queue <ll> pq;
#define INF 1000000000
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define mod 1000000007
#define itr ::iterator it
#define lcm(a,b) ((a)*(b))/gcd((a),(b))
#define remin(a,b) (a) = min((a),(b))
#define remax(a,b) (a) = max((a),(b))
#define db double
#define el "\n"
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
ll int gcd(ll int a, ll int b){
    if(a==0){
    	return 0;
    }
    if (b == 0){
       return 1;
    }
    return gcd(b, a % b);
}
ll int power(ll int x,ll int n){
	ll ans=1;
     while(n>0){
         if(n&1) ans=(ans*x)%mod;
         x=(x*x)%mod;
         n/=2;
     }
     if(x==0)ans=0;
     return ans;
}
int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r" , stdin);
	freopen("output.txt", "w" , stdout);
	#endif
	int t=1;
	//cin>>t;
	while(t--){
		ll int n,k,i,j;
		cin>>n>>k;
		int a[n];
		unordered_map<int,int> m;
		for(i=0;i<n;i++){
			cin>>a[i];
			m[a[i]]++;
		}
		sort(a,a+n);
		ll int count=0,ans=INT_MAX,flag=0,count2;
		for(i=0;i<n;i++){
			//if(a[i]==0)continue;
			count=m[a[i]];
			if(count==k){
				ans=0;
				break;
			}
			//cout<<a[i]<<" "<<count<<" yo boy"<<endl;
			count2=0;
			flag=0;
			for(j=1;a[i]*pow(2,j)<=a[n-1];j++){
				ll int tmp=a[i]*pow(2,j);
				//cout<<tmp<<endl;
				if(m[tmp]!=0){
					//cout<<tmp<<"hello"<<endl;
					count+=m[tmp];
					//cout<<count<<" hi"<<endl;
					if(count>=k){
						flag=1;
						count2+=(m[tmp]*j-(count-k)*j);
						//cout<<a[i]<<" "<<count2<<"   hihi"<<endl;
						break;
					}
					else{
						count2+=(m[tmp]*j);
					}
				}
				if(m[tmp+1]!=0){
					count+=m[tmp+1];
					if(count>=k){
						flag=1;
						count2+=(m[tmp+1]*j-(count-k)*j);
						//cout<<a[i]<<" hi "<<count2<<endl;
						break;
					}
					else{
						count2+=(m[tmp+1]*j);
					}
				}
			}
			if(flag==1){
				ans=min(ans,count2);
			}
		}
		if(ans!=INT_MAX)cout<<ans;
		else{
			ans=0;
			for(i=0;i<k;i++){
				ans+=ceil(log(a[i])/(double)log(2));
			}
			cout<<ans;
		}
	}
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<string>
#include<cmath>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<fstream>
#include<map>
#include<bitset>
#include<unordered_map>
#include<cstring>
#include<cctype>
using namespace std;
#define  ll long long
#define  ull unsigned long long
#define  ul unsigned long
#define all(v) v.begin(),v.end()
#define sz(n) (int)n.size()
#define pb(s) push_back(s);
#define format(n) fixed<<setprecision(n)
#define finl "\n"
#define Mohammed_Atef_Hassan fast();
#define mod 1000000007
#define INF LLONG_MAX
#define PI acos(-1)
#define clr(dp,n) memset(dp,n,sizeof dp)
#define ex(n) return cout<<n<<finl,0;
int dx[] = { 0, 1, 0, -1, 1, 1, -1, -1 };
int dy[] = { 1, 0, -1, 0, 1, -1, 1, -1 };
ll fix_mod(ll x, ll y)
{
	return (y + x % y) % y;
}
void fast() {
	std::ios_base::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);

#ifdef ONLINE_JUDGE
	//freopen("crosses.in", "r", stdin);

	//freopen("crosses.out", "w", stdout);
#endif
#ifndef ONLINE_JUDGE
	freopen("input.in", "r", stdin);
	freopen("output.out", "w", stdout);
#endif
}
ll f[(int)2e5 + 10];
ll res[(int)2e5 + 10];
ll d[(int)2e5 + 10];
int main()
{
	Mohammed_Atef_Hassan
		int n,k; ll x;
	cin >>n>>k;
	for (int i = 0; i < n; i++)
	{
		cin >> x;
		f[x]++;
	}
	vector<pair<ll, ll>>st;
	for (int i = 1; i <=2e5; i++)
	{
		if (f[i] == 0)continue;
		d[i] = f[i];
		st.push_back({ f[i],i });
	}
	sort(all(st));
	for (auto v : st)
	{
		x = v.second;
		while (x>=0)
		{
			if (x != v.second&&d[x]<k)
			{
				d[x] += v.first;
			  res[x] += v.first;
			}
			if (x == 0)break;
			x /= 2;
		}
	}
	ll dd = LLONG_MAX;
	for (int i = 0; i <= 2e5; i++)
	{
		if (d[i] >= k)dd = min(dd, res[i]);
	}
	cout << dd << finl;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<string>
#include<cmath>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<fstream>
#include<map>
#include<bitset>
#include<unordered_map>
#include<cstring>
#include<cctype>
using namespace std;
#define  ll long long
#define  ull unsigned long long
#define  ul unsigned long
#define all(v) v.begin(),v.end()
#define sz(n) (int)n.size()
#define pb(s) push_back(s);
#define format(n) fixed<<setprecision(n)
#define finl "\n"
#define Mohammed_Atef_Hassan fast();
#define mod 1000000007
#define INF LLONG_MAX
#define PI acos(-1)
#define clr(dp,n) memset(dp,n,sizeof dp)
#define ex(n) return cout<<n<<finl,0;
int dx[] = { 0, 1, 0, -1, 1, 1, -1, -1 };
int dy[] = { 1, 0, -1, 0, 1, -1, 1, -1 };
ll fix_mod(ll x, ll y)
{
	return (y + x % y) % y;
}
void fast() {
	std::ios_base::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);

#ifdef ONLINE_JUDGE
	//freopen("crosses.in", "r", stdin);

	//freopen("crosses.out", "w", stdout);
#endif
#ifndef ONLINE_JUDGE
	freopen("input.in", "r", stdin);
	freopen("output.out", "w", stdout);
#endif
}
ll f[(int)2e5 + 10];
ll res[(int)2e5 + 10];
ll d[(int)2e5 + 10];
int main()
{
	Mohammed_Atef_Hassan
		int n, k; ll x;
	cin >> n >> k;
	for (int i = 0; i < n; i++)
	{
		cin >> x;
		f[x]++;
	}
	vector<pair<ll, ll>>st;
	for (int i = 1; i <= 2e5; i++)
	{
		if (f[i] == 0)continue;
		d[i] = f[i];
		st.push_back({ f[i],i });
	}
	sort(all(st));
	for (auto v : st)
	{
		x = v.second; ll c = 0;
		while (x >= 0)
		{
			if (x != v.second&&d[x] < k)
			{
				d[x] +=v.first;
				res[x] +=c*v.first;
			}
			if (x == 0)break;
			x /= 2; c++;
		}
	}
	ll dd = LLONG_MAX;
	for (int i = 0; i <= 2e5; i++)
	{
		if (d[i] >= k)dd = min(dd, res[i]);
	}
	cout << dd << finl;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<string>
#include<cmath>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<fstream>
#include<map>
#include<bitset>
#include<unordered_map>
#include<cstring>
#include<cctype>
using namespace std;
#define  ll long long
#define  ull unsigned long long
#define  ul unsigned long
#define all(v) v.begin(),v.end()
#define sz(n) (int)n.size()
#define pb(s) push_back(s);
#define format(n) fixed<<setprecision(n)
#define finl "\n"
#define Mohammed_Atef_Hassan fast();
#define mod 1000000007
#define INF LLONG_MAX
#define PI acos(-1)
#define clr(dp,n) memset(dp,n,sizeof dp)
#define ex(n) return cout<<n<<finl,0;
int dx[] = { 0, 1, 0, -1, 1, 1, -1, -1 };
int dy[] = { 1, 0, -1, 0, 1, -1, 1, -1 };
ll fix_mod(ll x, ll y)
{
	return (y + x % y) % y;
}
void fast() {
	std::ios_base::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);

#ifdef ONLINE_JUDGE
	//freopen("crosses.in", "r", stdin);

	//freopen("crosses.out", "w", stdout);
#endif
#ifndef ONLINE_JUDGE
	freopen("input.in", "r", stdin);
	freopen("output.out", "w", stdout);
#endif
}
ll f[(int)2e5 + 10];
ll res[(int)2e5 + 10];
ll d[(int)2e5 + 10];
int main()
{
	Mohammed_Atef_Hassan
		int n, k; ll x;
	cin >> n >> k;
	for (int i = 0; i < n; i++)
	{
		cin >> x;
		f[x]++;
	}
	vector<pair<ll, ll>>st;
	for (int i = 1; i <= 2e5; i++)
	{
		if (f[i] == 0)continue;
		d[i] = f[i];
		st.push_back({ i,f[i]});
	}
	for (auto v : st)
	{
		x = v.first; ll c = 0;
		while (x >= 0)
		{
			if (x != v.first&&d[x] < k)
			{
				ll z = min(k - d[x],v.second);
				d[x] += z;
				res[x] += c * z;
			}
			if (x == 0)break;
			x /= 2; c++;
		}
	}
	ll dd = LLONG_MAX;
	for (int i = 0; i <= 2e5; i++)
	{
		if (d[i] >= k)dd = min(dd, res[i]);
	}
	cout << dd << finl;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<string>
#include<cmath>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<fstream>
#include<map>
#include<bitset>
#include<unordered_map>
#include<cstring>
#include<cctype>
using namespace std;
#define  ll long long
#define  ull unsigned long long
#define  ul unsigned long
#define all(v) v.begin(),v.end()
#define sz(n) (int)n.size()
#define pb(s) push_back(s);
#define format(n) fixed<<setprecision(n)
#define finl "\n"
#define Mohammed_Atef_Hassan fast();
#define mod 1000000007
#define INF LLONG_MAX
#define PI acos(-1)
#define clr(dp,n) memset(dp,n,sizeof dp)
#define ex(n) return cout<<n<<finl,0;
int dx[] = { 0, 1, 0, -1, 1, 1, -1, -1 };
int dy[] = { 1, 0, -1, 0, 1, -1, 1, -1 };
ll fix_mod(ll x, ll y)
{
	return (y + x % y) % y;
}
void fast() {
	std::ios_base::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);

#ifdef ONLINE_JUDGE
	//freopen("crosses.in", "r", stdin);

	//freopen("crosses.out", "w", stdout);
#endif
#ifndef ONLINE_JUDGE
	freopen("input.in", "r", stdin);
	freopen("output.out", "w", stdout);
#endif
}
ll f[(int)2e5 + 10];
ll res[(int)2e5 + 10];
ll d[(int)2e5 + 10];
int main()
{
	Mohammed_Atef_Hassan
		int n, k; ll x;
	cin >> n >> k;
	for (int i = 0; i < n; i++)
	{
		cin >> x;
		f[x]++;
	}
	vector<pair<ll, ll>>st;
	for (int i = 1; i <= 2e5; i++)
	{
		if (f[i] == 0)continue;
		d[i] = f[i];
		st.push_back({ f[i],i });
	}
	sort(all(st));
	for (auto v : st)
	{
		x = v.second; ll c = 0;
		while (x >= 0)
		{
			if (x != v.second&&d[x] < k)
			{
				ll z = min(k - d[x], v.first);
				d[x] += z;
				res[x] += c * z;
			}
			if (x == 0)break;
			x /= 2; c++;
		}
	}
	ll dd = LLONG_MAX;
	for (int i = 0; i <= 2e5; i++)
	{
		if (d[i] >= k)dd = min(dd, res[i]);
	}
	cout << dd << finl;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<string>
#include<cmath>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<fstream>
#include<map>
#include<bitset>
#include<unordered_map>
#include<cstring>
#include<cctype>
using namespace std;
#define  ll long long
#define  ull unsigned long long
#define  ul unsigned long
#define all(v) v.begin(),v.end()
#define sz(n) (int)n.size()
#define pb(s) push_back(s);
#define format(n) fixed<<setprecision(n)
#define finl "\n"
#define Mohammed_Atef_Hassan fast();
#define mod 1000000007
#define INF LLONG_MAX
#define PI acos(-1)
#define clr(dp,n) memset(dp,n,sizeof dp)
#define ex(n) return cout<<n<<finl,0;
int dx[] = { 0, 1, 0, -1, 1, 1, -1, -1 };
int dy[] = { 1, 0, -1, 0, 1, -1, 1, -1 };
ll fix_mod(ll x, ll y)
{
	return (y + x % y) % y;
}
void fast() {
	std::ios_base::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);

#ifdef ONLINE_JUDGE
	//freopen("crosses.in", "r", stdin);

	//freopen("crosses.out", "w", stdout);
#endif
#ifndef ONLINE_JUDGE
	freopen("input.in", "r", stdin);
	freopen("output.out", "w", stdout);
#endif
}
ll f[(int)2e5 + 10];
ll cnt[(int)2e5 + 10];
ll d[(int)2e5 + 10];
int main()
{
	Mohammed_Atef_Hassan
		int n, k; ll x;
	cin >> n >> k;
	ll res = LLONG_MAX;
	for (int i = 0; i < n; i++)
	{
		cin >> x;
		f[x]++;
		if (f[x] >= k)res = 0;
	}
	for (int i = 2e5; i >0; i--)
	{
		if (f[i] == 0)continue;
		x = i / 2; int prev = i;
		d[i] = f[i];
		while (x >= 0)
		{
			d[x] = d[prev] + f[x];
			cnt[x] = f[prev]+cnt[prev];
			if (d[x] >= k)res = min(res, cnt[x]);
			cnt[prev] = 0;
			prev = x;
			if (x == 0)break;
			x /= 2;
		}
	}
	cout << res << finl;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<string>
#include<cmath>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<fstream>
#include<map>
#include<bitset>
#include<unordered_map>
#include<cstring>
#include<cctype>
using namespace std;
#define  ll long long
#define  ull unsigned long long
#define  ul unsigned long
#define all(v) v.begin(),v.end()
#define sz(n) (int)n.size()
#define pb(s) push_back(s);
#define format(n) fixed<<setprecision(n)
#define finl "\n"
#define Mohammed_Atef_Hassan fast();
#define mod 1000000007
#define INF LLONG_MAX
#define PI acos(-1)
#define clr(dp,n) memset(dp,n,sizeof dp)
#define ex(n) return cout<<n<<finl,0;
int dx[] = { 0, 1, 0, -1, 1, 1, -1, -1 };
int dy[] = { 1, 0, -1, 0, 1, -1, 1, -1 };
ll fix_mod(ll x, ll y)
{
	return (y + x % y) % y;
}
void fast() {
	std::ios_base::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);

#ifdef ONLINE_JUDGE
	//freopen("crosses.in", "r", stdin);

	//freopen("crosses.out", "w", stdout);
#endif
#ifndef ONLINE_JUDGE
	freopen("input.in", "r", stdin);
	freopen("output.out", "w", stdout);
#endif
}
ll f[(int)2e5 + 10];
ll res[(int)2e5 + 10];
ll d[(int)2e5 + 10];
int main()
{
	Mohammed_Atef_Hassan
		int n, k; ll x;
	cin >> n >> k;
	for (int i = 0; i < n; i++)
	{
		cin >> x;
		f[x]++;
	}
	vector<pair<ll, ll>>st;
	for (int i = 1; i <= 2e5; i++)
	{
		if (f[i] == 0)continue;
		d[i] = f[i];
		st.push_back({ i,f[i]});
	}
	sort(all(st));
	for (auto v : st)
	{
		x = v.first; ll c = 0;
		while (x >= 0)
		{
			if (x != v.first&&d[x] < k)
			{
				ll z = min(k - d[x],v.second);
				d[x] += z;
				res[x] += c * z;
			}
			if (x == 0)break;
			x /= 2; c++;
		}
	}
	ll dd = LLONG_MAX;
	for (int i = 0; i <= 2e5; i++)
	{
		if (d[i] >= k)dd = min(dd, res[i]);
	}
	cout << dd << finl;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<string>
#include<cmath>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<fstream>
#include<map>
#include<bitset>
#include<unordered_map>
#include<cstring>
#include<cctype>
using namespace std;
#define  ll long long
#define  ull unsigned long long
#define  ul unsigned long
#define all(v) v.begin(),v.end()
#define sz(n) (int)n.size()
#define pb(s) push_back(s);
#define format(n) fixed<<setprecision(n)
#define finl "\n"
#define Mohammed_Atef_Hassan fast();
#define mod 1000000007
#define INF LLONG_MAX
#define PI acos(-1)
#define clr(dp,n) memset(dp,n,sizeof dp)
#define ex(n) return cout<<n<<finl,0;
int dx[] = { 0, 1, 0, -1, 1, 1, -1, -1 };
int dy[] = { 1, 0, -1, 0, 1, -1, 1, -1 };
ll fix_mod(ll x, ll y)
{
	return (y + x % y) % y;
}
void fast() {
	std::ios_base::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);

#ifdef ONLINE_JUDGE
	//freopen("crosses.in", "r", stdin);

	//freopen("crosses.out", "w", stdout);
#endif
#ifndef ONLINE_JUDGE
	freopen("input.in", "r", stdin);
	freopen("output.out", "w", stdout);
#endif
}
ll f[(int)2e5 + 10];
ll cnt[(int)2e5 + 10];
int main()
{
	Mohammed_Atef_Hassan
		int n, k; ll x;
	cin >> n >> k;
	ll res = LLONG_MAX;
	for (int i = 0; i < n; i++)
	{
		cin >> x;
		f[x]++;
		if (f[x] >= k)res = 0;
	}
	for (int i = 2e5; i >0; i--)
	{
		if (f[i] == 0)continue;
		x = i / 2;
		cnt[x] += f[i];
		f[x] += f[i];
		if (f[x] >= k)res = min(res, cnt[x]);
	}
	if (f[0])res = min(res, cnt[0]);
	cout << res << finl;
}


#include<bits/stdc++.h>
using namespace std;
long long a[200005][20],b[200005][20];
int main()
{
    int n,k,x;
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        int ct=0;
        while(x)
        {
            a[x][ct]++;
            b[x][ct]++;
            ct++;
            x/=2;
        }
        a[x][ct]++;b[x][ct]++;
    }
    for(int i=0;i<100005;i++)
    {
        for(int j=1;j<20;j++)
        a[i][j]+=a[i][j-1];
    }
    int ans=30;
    for(int i=0;i<100005;i++)
    {
        int low=0,high=19,mid,pos=-1;
        while(low<=high)
        {
            mid=(low+high)/2;
            if(a[i][mid]>=k) pos=mid,high=mid-1;
            else low=mid+1;
        }
        if(pos!=-1)
        {
            if(!pos)
            {
                ans=0;
                break;
            }
            int val=0,ele=a[i][pos-1],need=k-ele;
            for(int j=0;j<pos;j++)
            val+=(j*b[i][j]);
            val+=(need*pos);
            ans=min(val,ans);
        }
    }
    printf("%d",ans);
}


#include<bits/stdc++.h>
using namespace std;
int a[100005][20],b[100005][20];
int main()
{
    int n,k,x;
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        int ct=0;
        while(x)
        {
            a[x][ct]++;
            b[x][ct]++;
            ct++;
            x/=2;
        }
        a[x][ct]++;b[x][ct]++;
    }
    for(int i=0;i<=100005;i++)
    {
        for(int j=1;j<20;j++)
        a[i][j]+=a[i][j-1];
    }
    int ans=30;
    for(int i=0;i<=100005;i++)
    {
        int low=0,high=19,mid,pos=-1;
        while(low<=high)
        {
            mid=(low+high)/2;
            if(a[i][mid]>=k) pos=mid,high=mid-1;
            else low=mid+1;
        }
        if(pos!=-1)
        {
            if(!pos)
            {
                ans=0;
                break;
            }
            int val=0,ele=a[i][pos-1],need=k-ele;
            for(int j=0;j<pos;j++)
            val+=(j*b[i][j]);
            val+=(need*pos);
            ans=min(val,ans);
        }
    }
    printf("%d",ans);
}


#include<bits/stdc++.h>
using namespace std;
long long a[100005][20],b[100005][20];
int main()
{
    int n,k,x;
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        int ct=0;
        while(x)
        {
            a[x][ct]++;
            b[x][ct]++;
            ct++;
            x/=2;
        }
        a[x][ct]++;b[x][ct]++;
    }
    for(int i=0;i<100005;i++)
    {
        for(int j=1;j<20;j++)
        a[i][j]+=a[i][j-1];
    }
    int ans=30;
    for(int i=0;i<100005;i++)
    {
        int low=0,high=19,mid,pos=-1;
        while(low<=high)
        {
            mid=(low+high)/2;
            if(a[i][mid]>=k) pos=mid,high=mid-1;
            else low=mid+1;
        }
        if(pos!=-1)
        {
            if(!pos)
            {
                ans=0;
                break;
            }
            int val=0,ele=a[i][pos-1],need=k-ele;
            for(int j=0;j<pos;j++)
            val+=(j*b[i][j]);
            val+=(need*pos);
            ans=min(val,ans);
        }
    }
    printf("%d",ans);
}


#include<bits/stdc++.h>
using namespace std;
long long a[200005][20],b[200005][20];
int main()
{
    int n,k,x;
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        int ct=0;
        while(x)
        {
            a[x][ct]++;
            b[x][ct]++;
            ct++;
            x/=2;
        }
        a[x][ct]++;b[x][ct]++;
    }
    for(int i=0;i<200005;i++)
    {
        for(int j=1;j<20;j++)
        a[i][j]+=a[i][j-1];
    }
    int ans=1000000000;
    for(int i=0;i<200005;i++)
    {
        int low=0,high=19,mid,pos=-1;
        while(low<=high)
        {
            mid=(low+high)/2;
            if(a[i][mid]>=k) pos=mid,high=mid-1;
            else low=mid+1;
        }
        if(pos!=-1)
        {
            if(!pos)
            {
                ans=0;
                break;
            }
            int val=0,ele=a[i][pos-1],need=k-ele;
            for(int j=0;j<pos;j++)
            val+=(j*b[i][j]);
            val+=(need*pos);
            ans=min(val,ans);
        }
    }
    printf("%d\n",ans);
}


#include<bits/stdc++.h>
using namespace std;
long long a[100005][20],b[100005][20];
int main()
{
    int n,k,x;
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        int ct=0;
        while(x)
        {
            a[x][ct]++;
            b[x][ct]++;
            ct++;
            x/=2;
        }
        a[x][ct]++;b[x][ct]++;
    }
    for(int i=0;i<=100005;i++)
    {
        for(int j=1;j<20;j++)
        a[i][j]+=a[i][j-1];
    }
    int ans=30;
    for(int i=0;i<=100005;i++)
    {
        int low=0,high=19,mid,pos=-1;
        while(low<=high)
        {
            mid=(low+high)/2;
            if(a[i][mid]>=k) pos=mid,high=mid-1;
            else low=mid+1;
        }
        if(pos!=-1)
        {
            if(!pos)
            {
                ans=0;
                break;
            }
            int val=0,ele=a[i][pos-1],need=k-ele;
            for(int j=0;j<pos;j++)
            val+=(j*b[i][j]);
            val+=(need*pos);
            ans=min(val,ans);
        }
    }
    printf("%d",ans);
}


#include<bits/stdc++.h>
using namespace std;
int a[100005][20],b[100005][20];
int main()
{
    int n,k,x;
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        int ct=0;
        while(x)
        {
            a[x][ct]++;
            b[x][ct]++;
            ct++;
            x/=2;
        }
        a[x][ct]++;b[x][ct]++;
    }
    for(int i=0;i<=n;i++)
    {
        for(int j=1;j<20;j++)
        a[i][j]+=a[i][j-1];
    }
    int ans=30;
    for(int i=0;i<=n;i++)
    {
        int low=0,high=19,mid,pos=-1;
        while(low<=high)
        {
            mid=(low+high)/2;
            if(a[i][mid]>=k) pos=mid,high=mid-1;
            else low=mid+1;
        }
        if(pos!=-1)
        {
            if(!pos)
            {
                ans=0;
                break;
            }
            int val=0,ele=a[i][pos-1],need=k-ele;
            for(int j=0;j<pos;j++)
            val+=(j*b[i][j]);
            val+=(need*pos);
            ans=min(val,ans);
        }
    }
    printf("%d",ans);
}


#include<bits/stdc++.h>
using namespace std;
long long a[200005][20],b[200005][20];
int main()
{
    int n,k,x;
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        int ct=0;
        while(x)
        {
            a[x][ct]++;
            b[x][ct]++;
            ct++;
            x/=2;
        }
        a[x][ct]++;b[x][ct]++;
    }
    for(int i=0;i<100005;i++)
    {
        for(int j=1;j<20;j++)
        a[i][j]+=a[i][j-1];
    }
    int ans=1000000000;
    for(int i=0;i<100005;i++)
    {
        int low=0,high=19,mid,pos=-1;
        while(low<=high)
        {
            mid=(low+high)/2;
            if(a[i][mid]>=k) pos=mid,high=mid-1;
            else low=mid+1;
        }
        if(pos!=-1)
        {
            if(!pos)
            {
                ans=0;
                break;
            }
            int val=0,ele=a[i][pos-1],need=k-ele;
            for(int j=0;j<pos;j++)
            val+=(j*b[i][j]);
            val+=(need*pos);
            ans=min(val,ans);
        }
    }
    printf("%d",ans);
}


// ------- PBDS ------ //
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
// ------- PBDS ------ //

#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
#define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define testcase ll tt; cin>>tt; while(tt--)
#define pb push_back
#define nn cout<<"\n"
#define MOD 1000000007
#define pi 3.1415926535
#define endl "\n"
#define vll vector<ll>
#define MULTI int T; cin >> T; while(T--)
#define all(a)      (a).begin(),(a).end()
#define here cout<<"I'm here\n";
void vout(vector<ll> ans){
    for(auto ele:ans) cout<<ele<<" "; cout<<endl;
}
vector<ll> adj[200001];
bool vis[200001];

// ------- PBDS ------ //
typedef tree<
// change this for any data-type
string,
null_type,
// change this for any data-type
less<string>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

/*
    to get the position of an element in set , do
    s.order_of_key(ele);
    this returns the number of elements strictly lesser than ele in set s
    Let us assume we have a set s : {1, 5, 6, 17, 88}, then :
    s.order_of_key(6) : Count of elements strictly smaller than 6 is 2.
    s.order_of_key(25) : Count of elements strictly smaller than 25 is 4.

    to get the element at position k
    s.find_by_order(k);
    returns an iterator to the ele counting from 0
    Let us assume we have a set s : {1, 5, 6, 17, 88}, then :
    *(s.find_by_order(2)) : 3rd element in the set i.e. 6
    *(s.find_by_order(4)) : 5th element in the set i.e. 88
*/

// ------- PBDS ------ //

clock_t time_p=clock();
void purvansh11()
{
    time_p=clock()-time_p;
    cerr<<"Time Taken : "<<(float)(time_p)/CLOCKS_PER_SEC<<"\n";
}

//BINARY EXPONENTIATION  -- O(log2(b))
ll power(ll a, ll b) {
    /*
        for 3^11 :
        as 11 -> 1011, we mean 3^8 * 3^2 * 3^1
        so multiply for all ones in binary rep of b
    */

    ll ans = 1;
    ll num = a;
    while (b > 0) {
        if (b & 1) ans = ((ans % MOD) * (num % MOD)) % MOD;
        num = ((num % MOD) * (num % MOD)) % MOD;
        b /= 2;
    }
    return ans;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    SPEED;
    cout.precision(8);
    cout << fixed;
    ll n,k;
    cin>>n>>k;
    vector<ll> v(n);
    for(auto &itr:v) cin>>itr;
    unordered_map<ll,multiset<ll>> mp;
    set<ll> is_there;
    for(auto ele:v){
        ll cnt = 0;
        while(ele!=0){
            is_there.insert(ele);
            mp[ele].insert(cnt++);
             ele/=2;
        }
        is_there.insert(0);
        mp[0].insert(cnt);
    }
    ll ans = LLONG_MAX;
    // cout<<mp[3].size()<<endl;
    for(auto num:is_there){
        // cout<<num<<" "<<mp[num].size()<<endl;
        if(mp[num].size() >= k){
            ll temp_ans = 0;
            auto itr = mp[num].begin();
            ll tk = k;
            while(tk!=0){
                temp_ans += (*itr);
                itr++;
                tk--;
            }
            ans = min(ans,temp_ans);
        }
    }
    cout<<ans;
    purvansh11();
    return 0;
}


// ------- PBDS ------ //
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
// ------- PBDS ------ //

#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
#define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define testcase ll tt; cin>>tt; while(tt--)
#define pb push_back
#define nn cout<<"\n"
#define MOD 1000000007
#define pi 3.1415926535
#define endl "\n"
#define vll vector<ll>
#define MULTI int T; cin >> T; while(T--)
#define all(a)      (a).begin(),(a).end()
#define here cout<<"I'm here\n";
void vout(vector<ll> ans){
    for(auto ele:ans) cout<<ele<<" "; cout<<endl;
}
vector<ll> adj[200001];
bool vis[200001];

// ------- PBDS ------ //
typedef tree<
// change this for any data-type
string,
null_type,
// change this for any data-type
less<string>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

/*
    to get the position of an element in set , do
    s.order_of_key(ele);
    this returns the number of elements strictly lesser than ele in set s
    Let us assume we have a set s : {1, 5, 6, 17, 88}, then :
    s.order_of_key(6) : Count of elements strictly smaller than 6 is 2.
    s.order_of_key(25) : Count of elements strictly smaller than 25 is 4.

    to get the element at position k
    s.find_by_order(k);
    returns an iterator to the ele counting from 0
    Let us assume we have a set s : {1, 5, 6, 17, 88}, then :
    *(s.find_by_order(2)) : 3rd element in the set i.e. 6
    *(s.find_by_order(4)) : 5th element in the set i.e. 88
*/

// ------- PBDS ------ //

clock_t time_p=clock();
void purvansh11()
{
    time_p=clock()-time_p;
    cerr<<"Time Taken : "<<(float)(time_p)/CLOCKS_PER_SEC<<"\n";
}

//BINARY EXPONENTIATION  -- O(log2(b))
ll power(ll a, ll b) {
    /*
        for 3^11 :
        as 11 -> 1011, we mean 3^8 * 3^2 * 3^1
        so multiply for all ones in binary rep of b
    */

    ll ans = 1;
    ll num = a;
    while (b > 0) {
        if (b & 1) ans = ((ans % MOD) * (num % MOD)) % MOD;
        num = ((num % MOD) * (num % MOD)) % MOD;
        b /= 2;
    }
    return ans;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    SPEED;
    cout.precision(8);
    cout << fixed;
    ll n,k;
    cin>>n>>k;
    vector<ll> v(n);
    for(auto &itr:v) cin>>itr;
    unordered_map<ll,multiset<ll>> mp;
    set<ll,greater<ll>> is_there;
    for(auto ele:v){
        ll cnt = 0;
        while(ele!=0){
            is_there.insert(ele);
            mp[ele].insert(cnt++);
            ele/=2;
        }
        is_there.insert(0);
        mp[0].insert(cnt);
    }
    ll ans = LLONG_MAX;
    // cout<<mp[3].size()<<endl;
    bool done = false;
    ll size=0;
    for(auto num:is_there){
        if(done and mp[num].size() > size) break;
        // cout<<num<<" "<<mp[num].size()<<endl;
        if(mp[num].size() >= k){
            done = true;
            size = mp[num].size();
            ll temp_ans = 0;
            auto itr = mp[num].begin();
            ll tk = k;
            while(tk!=0){
                temp_ans += (*itr);
                itr++;
                tk--;
            }
            ans = min(ans,temp_ans);
            break;
        }
    }
    cout<<ans;
    purvansh11();
    return 0;
}


// ------- PBDS ------ //
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
// ------- PBDS ------ //

#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
#define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define testcase ll tt; cin>>tt; while(tt--)
#define pb push_back
#define nn cout<<"\n"
#define MOD 1000000007
#define pi 3.1415926535
#define endl "\n"
#define vll vector<ll>
#define MULTI int T; cin >> T; while(T--)
#define all(a)      (a).begin(),(a).end()
#define here cout<<"I'm here\n";
void vout(vector<ll> ans){
    for(auto ele:ans) cout<<ele<<" "; cout<<endl;
}
vector<ll> adj[200001];
bool vis[200001];

// ------- PBDS ------ //
typedef tree<
// change this for any data-type
string,
null_type,
// change this for any data-type
less<string>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

/*
    to get the position of an element in set , do
    s.order_of_key(ele);
    this returns the number of elements strictly lesser than ele in set s
    Let us assume we have a set s : {1, 5, 6, 17, 88}, then :
    s.order_of_key(6) : Count of elements strictly smaller than 6 is 2.
    s.order_of_key(25) : Count of elements strictly smaller than 25 is 4.

    to get the element at position k
    s.find_by_order(k);
    returns an iterator to the ele counting from 0
    Let us assume we have a set s : {1, 5, 6, 17, 88}, then :
    *(s.find_by_order(2)) : 3rd element in the set i.e. 6
    *(s.find_by_order(4)) : 5th element in the set i.e. 88
*/

// ------- PBDS ------ //

clock_t time_p=clock();
void purvansh11()
{
    time_p=clock()-time_p;
    cerr<<"Time Taken : "<<(float)(time_p)/CLOCKS_PER_SEC<<"\n";
}

//BINARY EXPONENTIATION  -- O(log2(b))
ll power(ll a, ll b) {
    /*
        for 3^11 :
        as 11 -> 1011, we mean 3^8 * 3^2 * 3^1
        so multiply for all ones in binary rep of b
    */

    ll ans = 1;
    ll num = a;
    while (b > 0) {
        if (b & 1) ans = ((ans % MOD) * (num % MOD)) % MOD;
        num = ((num % MOD) * (num % MOD)) % MOD;
        b /= 2;
    }
    return ans;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    SPEED;
    cout.precision(8);
    cout << fixed;
    ll n,k;
    cin>>n>>k;
    vector<ll> v(n);
    for(auto &itr:v) cin>>itr;
    unordered_map<ll,vector<ll>> mp;
    set<ll> is_there;
    for(auto ele:v){
        ll cnt = 0;
        while(ele!=0){
            is_there.insert(ele);
            mp[ele].pb(cnt++);
             ele/=2;
        }
        is_there.insert(0);
        mp[0].pb(cnt);
    }
    ll ans = LLONG_MAX;
    // cout<<mp[3].size()<<endl;
    for(auto num:is_there){
        // cout<<num<<" "<<mp[num].size()<<endl;
        if(mp[num].size() >= k){
            sort(all(mp[num]));
            ll temp_ans = 0;
            auto itr = mp[num].begin();
            ll tk = k;
            while(tk!=0){
                temp_ans += (*itr);
                itr++;
                tk--;
            }
            ans = min(ans,temp_ans);
        }
    }
    cout<<ans;
    purvansh11();
    return 0;
}


// ------- PBDS ------ //
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
// ------- PBDS ------ //

#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
#define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define testcase ll tt; cin>>tt; while(tt--)
#define pb push_back
#define nn cout<<"\n"
#define MOD 1000000007
#define pi 3.1415926535
#define endl "\n"
#define vll vector<ll>
#define MULTI int T; cin >> T; while(T--)
#define all(a)      (a).begin(),(a).end()
#define here cout<<"I'm here\n";
void vout(vector<ll> ans){
    for(auto ele:ans) cout<<ele<<" "; cout<<endl;
}
vector<ll> adj[200001];
bool vis[200001];

// ------- PBDS ------ //
typedef tree<
// change this for any data-type
string,
null_type,
// change this for any data-type
less<string>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

/*
    to get the position of an element in set , do
    s.order_of_key(ele);
    this returns the number of elements strictly lesser than ele in set s
    Let us assume we have a set s : {1, 5, 6, 17, 88}, then :
    s.order_of_key(6) : Count of elements strictly smaller than 6 is 2.
    s.order_of_key(25) : Count of elements strictly smaller than 25 is 4.

    to get the element at position k
    s.find_by_order(k);
    returns an iterator to the ele counting from 0
    Let us assume we have a set s : {1, 5, 6, 17, 88}, then :
    *(s.find_by_order(2)) : 3rd element in the set i.e. 6
    *(s.find_by_order(4)) : 5th element in the set i.e. 88
*/

// ------- PBDS ------ //

clock_t time_p=clock();
void purvansh11()
{
    time_p=clock()-time_p;
    cerr<<"Time Taken : "<<(float)(time_p)/CLOCKS_PER_SEC<<"\n";
}

//BINARY EXPONENTIATION  -- O(log2(b))
ll power(ll a, ll b) {
    /*
        for 3^11 :
        as 11 -> 1011, we mean 3^8 * 3^2 * 3^1
        so multiply for all ones in binary rep of b
    */

    ll ans = 1;
    ll num = a;
    while (b > 0) {
        if (b & 1) ans = ((ans % MOD) * (num % MOD)) % MOD;
        num = ((num % MOD) * (num % MOD)) % MOD;
        b /= 2;
    }
    return ans;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    SPEED;
    cout.precision(8);
    cout << fixed;
    ll n,k;
    cin>>n>>k;
    vector<ll> v(n);
    for(auto &itr:v) cin>>itr;
    unordered_map<ll,vector<ll>> mp;
    set<ll,greater<ll>> is_there;
    for(auto ele:v){
        ll cnt = 0;
        while(ele!=0){
            is_there.insert(ele);
            mp[ele].pb(cnt++);
            ele/=2;
        }
        is_there.insert(0);
        mp[0].pb(cnt);
    }
    ll ans = LLONG_MAX;
    // cout<<mp[3].size()<<endl;
    for(auto num:is_there){
        // cout<<num<<" "<<mp[num].size()<<endl;
        if(mp[num].size() >= k){
            sort(all(mp[num]));
            ll temp_ans = 0;
            auto itr = mp[num].begin();
            ll tk = k;
            while(tk!=0){
                temp_ans += (*itr);
                itr++;
                tk--;
            }
            ans = min(ans,temp_ans);
        }
    }
    cout<<ans;
    purvansh11();
    return 0;
}


// ------- PBDS ------ //
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
// ------- PBDS ------ //

#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
#define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define testcase ll tt; cin>>tt; while(tt--)
#define pb push_back
#define nn cout<<"\n"
#define MOD 1000000007
#define pi 3.1415926535
#define endl "\n"
#define vll vector<ll>
#define MULTI int T; cin >> T; while(T--)
#define all(a)      (a).begin(),(a).end()
#define here cout<<"I'm here\n";
void vout(vector<ll> ans){
    for(auto ele:ans) cout<<ele<<" "; cout<<endl;
}
vector<ll> adj[200001];
bool vis[200001];

// ------- PBDS ------ //
typedef tree<
// change this for any data-type
string,
null_type,
// change this for any data-type
less<string>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

/*
    to get the position of an element in set , do
    s.order_of_key(ele);
    this returns the number of elements strictly lesser than ele in set s
    Let us assume we have a set s : {1, 5, 6, 17, 88}, then :
    s.order_of_key(6) : Count of elements strictly smaller than 6 is 2.
    s.order_of_key(25) : Count of elements strictly smaller than 25 is 4.

    to get the element at position k
    s.find_by_order(k);
    returns an iterator to the ele counting from 0
    Let us assume we have a set s : {1, 5, 6, 17, 88}, then :
    *(s.find_by_order(2)) : 3rd element in the set i.e. 6
    *(s.find_by_order(4)) : 5th element in the set i.e. 88
*/

// ------- PBDS ------ //

clock_t time_p=clock();
void purvansh11()
{
    time_p=clock()-time_p;
    cerr<<"Time Taken : "<<(float)(time_p)/CLOCKS_PER_SEC<<"\n";
}

//BINARY EXPONENTIATION  -- O(log2(b))
ll power(ll a, ll b) {
    /*
        for 3^11 :
        as 11 -> 1011, we mean 3^8 * 3^2 * 3^1
        so multiply for all ones in binary rep of b
    */

    ll ans = 1;
    ll num = a;
    while (b > 0) {
        if (b & 1) ans = ((ans % MOD) * (num % MOD)) % MOD;
        num = ((num % MOD) * (num % MOD)) % MOD;
        b /= 2;
    }
    return ans;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    SPEED;
    cout.precision(8);
    cout << fixed;
    ll n,k;
    cin>>n>>k;
    vector<ll> v(n);
    for(auto &itr:v) cin>>itr;
    unordered_map<ll,multiset<ll>> mp;
    set<ll,greater<ll>> is_there;
    for(auto ele:v){
        ll cnt = 0;
        while(ele!=0){
            is_there.insert(ele);
            mp[ele].insert(cnt++);
            ele/=2;
        }
        is_there.insert(0);
        mp[0].insert(cnt);
    }
    ll ans = LLONG_MAX;
    // cout<<mp[3].size()<<endl;
    for(auto num:is_there){
        // cout<<num<<" "<<mp[num].size()<<endl;
        if(mp[num].size() >= k){
            ll temp_ans = 0;
            auto itr = mp[num].begin();
            ll tk = k;
            while(tk!=0){
                temp_ans += (*itr);
                itr++;
                tk--;
            }
            ans = min(ans,temp_ans);
            break;
        }
    }
    cout<<ans;
    purvansh11();
    return 0;
}


#include <iostream>
#include <set>

using namespace std;
multiset<int> numbers[18][18];

int getTwoPower(int number)
{
    if (number == 0)
    {
        return 0;
    }

    int p = -1;

    do
    {
        p++;
    } while (number >>= 1);
    return p;
}

void addNumber(int number)
{
    int twoPower = getTwoPower(number);

    for(int i=twoPower; i>=0; i--){
        numbers[twoPower][i].insert(number);
        number>>=1;
    }

}

int calculateMinDivNumForPow(int k, int twoPow){
    int minDivNum=1e9;
    for(int el : numbers[twoPow][twoPow]){
        int currentCount = numbers[twoPow][twoPow].count(el);
        int divNum = 0;

        if(currentCount >=k){
            return 0;
        }

        for(int i=twoPow+1; i <=17; i++){
            int available = min((int) numbers[i][twoPow].count(el), k-currentCount);
            currentCount+=available;
            divNum+=(i-twoPow)*available;

            if(currentCount >=k){
                minDivNum = min(minDivNum, divNum);
                continue;
            }
        }

    }

    return minDivNum;
}

int main()
{

    int n, k;
    cin >> n >> k;

    for (int i = 0; i < n; i++)
    {
        int number;
        cin >> number;
        addNumber(number);
    }

    int answ = 1e9;

    for(int twoPow=0; twoPow<=17; twoPow++){
        answ = min(answ, calculateMinDivNumForPow(k, twoPow));
    }

    cout << answ << endl;

    return 0;
}


#include <iostream>
#include <set>

using namespace std;
multiset<int> numbers[18][18];

int getTwoPower(int number)
{
    if (number == 0)
    {
        return 0;
    }

    int p = -1;

    do
    {
        p++;
    } while (number >>= 1);
    return p;
}

void addNumber(int number)
{
    int twoPower = getTwoPower(number);

    for (int i = twoPower; i >= 0; i--)
    {
        numbers[twoPower][i].insert(number);
        number >>= 1;
    }
}

int minForEl(int el, int k)
{

    int twoPow = getTwoPower(el);
    int currentCount = 0;
    int divNum = 0;

    for (int i = twoPow; i < 18; i++)
    {
        int available = min((int)numbers[i][twoPow].count(el), k - currentCount);
        currentCount += available;
        divNum += (i - twoPow) * available;

        if (currentCount >= k)
        {
            return divNum;
        }
    }

    return 1e9;
}

int calculateMinDivNum(int k)
{
    int minDivNum = 1e9;

    for (int el = 1; el <= 2 * 1e5; el++)
    {
        minDivNum = min(minDivNum, minForEl(el, k));
    }
    return minDivNum;
}

int main()
{
    ios_base::sync_with_stdio(false);
    int n, k;
    cin >> n >> k;

    for (int i = 0; i < n; i++)
    {
        int number;
        cin >> number;
        addNumber(number);
    }

    cout << calculateMinDivNum(k) << endl;

    return 0;
}


#include <iostream>
#include <set>

using namespace std;
multiset<int> numbers[18][18];

int getTwoPower(int number)
{
    if (number == 0)
    {
        return 0;
    }

    int p = -1;

    do
    {
        p++;
    } while (number >>= 1);
    return p;
}

void addNumber(int number)
{
    int twoPower = getTwoPower(number);

    for (int i = twoPower; i >= 0; i--)
    {
        numbers[twoPower][i].insert(number);
        number >>= 1;
    }
}

int minForEl(int el, int k)
{

    int twoPow = getTwoPower(el);
    int currentCount = 0;
    int divNum = 0;

    for (int i = 0; i < 18; i++)
    {
        int available = min((int)numbers[i][twoPow].count(el), k - currentCount);
        currentCount += available;
        divNum += (i - twoPow) * available;

        if (currentCount >= k)
        {
            return divNum;
        }
    }

    return 1e9;
}

int calculateMinDivNum(int k)
{
    int minDivNum = 1e9;

    for (int el = 1; el <= 2 * 1e5; el++)
    {
        minDivNum = min(minDivNum, minForEl(el, k));
    }
    return minDivNum;
}

int main()
{
    ios_base::sync_with_stdio(false);

    int n, k;
    cin >> n >> k;

    for (int i = 0; i < n; i++)
    {
        int number;
        cin >> number;
        addNumber(number);
    }

    cout << calculateMinDivNum(k) << endl;

    return 0;
}


#include <iostream>
#include <set>

using namespace std;
multiset<int> numbers[18][18];

int getTwoPower(int number)
{
    if (number == 0)
    {
        return 0;
    }

    int p = -1;

    do
    {
        p++;
    } while (number >>= 1);
    return p;
}

void addNumber(int number)
{
    int twoPower = getTwoPower(number);

    for (int i = twoPower; i >= 0; i--)
    {
        numbers[twoPower][i].insert(number);
        number >>= 1;
    }
}

int minForEl(int el, int k)
{

    int twoPow = getTwoPower(el);
    int currentCount = 0;
    int divNum = 0;

    for (int i = 0; i < 18; i++)
    {
        int available = min((int)numbers[i][twoPow].count(el), k - currentCount);
        currentCount += available;
        divNum += (i - twoPow) * available;

        if (currentCount >= k)
        {
            return divNum;
        }
    }

    return 1e9;
}

int calculateMinDivNum(int k)
{
    int minDivNum = 1e9;

    for (int y = 0; y < 18; y++)
    {
        for (int x = 0; x < 18; x++)
        {
            for (int el : numbers[y][x])
            {
                minDivNum = min(minDivNum, minForEl(el, k));
            }
        }
    }
    return minDivNum;
}

int main()
{
    ios_base::sync_with_stdio(false);

    int n, k;
    cin >> n >> k;

    for (int i = 0; i < n; i++)
    {
        int number;
        cin >> number;
        addNumber(number);
    }

    cout << calculateMinDivNum(k) << endl;

    return 0;
}


#include <iostream>
#include <set>

using namespace std;
multiset<int> numbers[18][18];

int getTwoPower(int number)
{
    if (number == 0)
    {
        return 0;
    }

    int p = -1;

    do
    {
        p++;
    } while (number >>= 1);
    return p;
}

void addNumber(int number)
{
    int twoPower = getTwoPower(number);

    for (int i = twoPower; i >= 0; i--)
    {
        numbers[twoPower][i].insert(number);
        number >>= 1;
    }
}

int minForEl(int el, int k)
{

    int twoPow = getTwoPower(el);
    int currentCount = 0;
    int divNum = 0;

    for (int i = 0; i < 18; i++)
    {
        int available = min((int)numbers[i][twoPow].count(el), k - currentCount);
        currentCount += available;
        divNum += (i - twoPow) * available;

        if (currentCount >= k)
        {
            return divNum;
        }
    }

    return 1e9;
}

int calculateMinDivNum(int k)
{
    int minDivNum = 1e9;

    for (int el = 1; el <= 2 * 1e5; el++)
    {
        minDivNum = min(minDivNum, minForEl(el, k));
    }
    return minDivNum;
}

int main()
{

    int n, k;
    cin >> n >> k;

    for (int i = 0; i < n; i++)
    {
        int number;
        cin >> number;
        addNumber(number);
    }

    cout << calculateMinDivNum(k) << endl;

    return 0;
}


#include <iostream>
#include <set>

using namespace std;
multiset<int> numbers[18][18];

int getTwoPower(int number)
{
    if (number == 0)
    {
        return 0;
    }

    int p = -1;

    do
    {
        p++;
    } while (number >>= 1);
    return p;
}

void addNumber(int number)
{
    int twoPower = getTwoPower(number);

    for(int i=twoPower; i>=0; i--){
        numbers[twoPower][i].insert(number);
        number>>=1;
    }

}

int minForEl(int el, int k){

    int twoPow = getTwoPower(el);
    int currentCount = 0;
    int divNum = 0;

    for(int i=0; i <18; i++){
        int available = min((int)numbers[i][twoPow].count(el), k-currentCount);
        currentCount+=available;
        divNum+=(i-twoPow)*available;

         if(currentCount >=k){
            return  divNum;
        }
    }

    return 1e9;
}

int calculateMinDivNum(int k){
    int minDivNum=1e9;

    for(int y=0; y<18; y++){
        for(int x=0; x<18; x++){
            for(int el : numbers[y][x]){
                minDivNum = min(minDivNum, minForEl(el, k));
            }
        }
    }
    return minDivNum;
}

int main()
{

    int n, k;
    cin >> n >> k;

    for (int i = 0; i < n; i++)
    {
        int number;
        cin >> number;
        addNumber(number);
    }

    cout << calculateMinDivNum(k) << endl;

    return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define int long long int
#define ssd ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mod 1e9+7
#define ordered_set tree<ll, null_type, less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define trace(x) cerr<<#x<<" : "<<x<<endl;
using namespace std;
using namespace __gnu_pbds;

signed main()
{
    ssd
    int i,j,k,n,m,maxu=0;
    cin>>n>>k;
    vector<int> v(n);
    unordered_map<int,multiset<int>> mp;
    vector<int> ans(200005,0);
    for(i=0;i<n;i++)
    {
        cin>>v[i];
        maxu=max(maxu,v[i]);
        mp[v[i]].insert(0);
    }
    for(i=0;i<n;i++)
    {
        int a=v[i];
        int c=0;
        while(a>0)
        {
            c++;
            a/=2;
            mp[a].insert(c);
            ans[a]+=c;
            if(mp[a].size()>k)
            {
                auto it=mp[a].end();it--;
                ans[a]-=*it;
                mp[a].erase(it);
            }
            if(a==0) break;
        }
    }
    int minu = INT_MAX;
    for(i=0;i<=maxu;i++)
    {
        if(mp[i].size()==k)
        {
            minu = min(minu,ans[i]);
        }
    }
    cout<<minu;
    return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define ll long long int
#define ssd ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mod 1e9+7
#define ordered_set tree<ll, null_type, less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define trace(x) cerr<<#x<<" : "<<x<<endl;
using namespace std;
using namespace __gnu_pbds;

signed main()
{
    ssd
    int i,j,k,n,m,maxu=0;
    cin>>n>>k;
    vector<int> v(n);
    unordered_map<int,multiset<int>> mp;
    vector<int> ans(200005,0);
    int minu = INT_MAX;
    for(i=0;i<n;i++)
    {
        cin>>v[i];
        maxu=max(maxu,v[i]);
        mp[v[i]].insert(0);
        if(mp[v[i]].size()>=k)
        {
            minu = 0;
        }
    }
    if(minu==0)
    {
        cout<<0;
        return 0;
    }
    for(i=0;i<n;i++)
    {
        int a=v[i];
        int c=0;
        while(a>0)
        {
            c++;
            a/=2;
            mp[a].insert(c);
            ans[a]+=c;
            if(mp[a].size()>k)
            {
                auto it=mp[a].end();it--;
                ans[a]-=*it;
                mp[a].erase(it);
            }
            if(mp[a].size()>=k)
            {
                minu = min(minu,ans[a]);
            }
            if(a==0) break;
        }
    }
    cout<<minu;
    return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define ll long long int
#define ssd ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mod 1e9+7
#define ordered_set tree<ll, null_type, less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define trace(x) cerr<<#x<<" : "<<x<<endl;
using namespace std;
using namespace __gnu_pbds;

signed main()
{
    ssd
    int i,j,k,n,m,maxu=0;
    cin>>n>>k;
    vector<int> v(n);
    unordered_map<int,multiset<int>> mp;
    vector<int> ans(200005,0);
    for(i=0;i<n;i++)
    {
        cin>>v[i];
        maxu=max(maxu,v[i]);
        mp[v[i]].insert(0);
    }
    for(i=0;i<n;i++)
    {
        int a=v[i];
        int c=0;
        while(a>0)
        {
            c++;
            a/=2;
            mp[a].insert(c);
            ans[a]+=c;
            if(mp[a].size()>k)
            {
                auto it=mp[a].end();it--;
                ans[a]-=*it;
                mp[a].erase(it);
            }
            if(a==0) break;
        }
    }
    int minu = INT_MAX;
    for(i=0;i<=maxu;i++)
    {
        if(mp[i].size()>=k)
        {
            minu = min(minu,ans[i]);
        }
    }
    cout<<minu;
    return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define ll long long int
#define ssd ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mod 1e9+7
#define ordered_set tree<ll, null_type, less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define trace(x) cerr<<#x<<" : "<<x<<endl;
using namespace std;
using namespace __gnu_pbds;

signed main()
{
    ssd
    int i,j,k,n,m,maxu=0;
    cin>>n>>k;
    vector<int> v(n);
    vector<multiset<int>> mp(200005);
    vector<int> ans(200005,0);
    int minu = INT_MAX;
    for(i=0;i<n;i++)
    {
        cin>>v[i];
        maxu=max(maxu,v[i]);
        mp[v[i]].insert(0);
        if(mp[v[i]].size()>=k) minu = 0;
    }
    if(minu==0)
    {
        cout<<0;
        return 0;
    }
    for(i=0;i<n;i++)
    {
        int a=v[i];
        int c=0;
        while(a>0)
        {
            c++;
            a/=2;
            mp[a].insert(c);
            ans[a]+=c;
            if(mp[a].size()>k)
            {
                auto it=mp[a].end();it--;
                ans[a]-=*it;
                mp[a].erase(it);
            }
            if(mp[a].size()>=k)
            {
                minu = min(minu,ans[a]);
            }
            if(a==0) break;
        }
    }
    cout<<minu;
    return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define int long long int
#define ssd ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mod 1e9+7
#define ordered_set tree<ll, null_type, less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define trace(x) cerr<<#x<<" : "<<x<<endl;
using namespace std;
using namespace __gnu_pbds;

signed main()
{
    ssd
    int i,j,k,n,m,maxu=0;
    cin>>n>>k;
    vector<int> v(n);
    unordered_map<int,multiset<int>> mp;
    vector<int> ans(200005,0);
    for(i=0;i<n;i++)
    {
        cin>>v[i];
        maxu=max(maxu,v[i]);
        mp[v[i]].insert(0);
    }
    for(i=0;i<n;i++)
    {
        int a=v[i];
        int c=0;
        while(a>0)
        {
            c++;
            a/=2;
            mp[a].insert(c);
            ans[a]+=c;
            if(mp[a].size()>k)
            {
                auto it=mp[a].end();it--;
                ans[a]-=*it;
                mp[a].erase(it);
            }
            if(a==0) break;
        }
    }
    int minu = INT_MAX;
    for(i=0;i<=maxu;i++)
    {
        if(mp[i].size()>=k)
        {
            minu = min(minu,ans[i]);
        }
    }
    cout<<minu;
    return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define int long long int
#define ssd ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mod 1e9+7
#define ordered_set tree<ll, null_type, less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define trace(x) cerr<<#x<<" : "<<x<<endl;
using namespace std;
using namespace __gnu_pbds;

signed main()
{
    ssd
    int i,j,k,n,m,maxu=0;
    cin>>n>>k;
    vector<int> v(n);
    unordered_map<int,multiset<int>> mp;
    vector<int> ans(200005,0);
    for(i=0;i<n;i++)
    {
        cin>>v[i];
        maxu=max(maxu,v[i]);
        mp[v[i]].insert(0);
    }
    for(i=0;i<n;i++)
    {
        int a=v[i];
        int c=0;
        while(a>0)
        {
            c++;
            a/=2;
            mp[a].insert(c);
            ans[a]+=c;
            if(mp[a].size()>k)
            {
                auto it=mp[a].end();it--;
                ans[a]-=*it;
                mp[a].erase(it);
            }
            if(a==0)
            {
                mp[a].insert(c);
                ans[a]+=c;
                if(mp[a].size()>k)
                {
                    auto it=mp[a].end();it--;
                    ans[a]-=*it;
                    mp[a].erase(it);
                }
                break;
            }
        }
    }
    int minu = INT_MAX;
    for(i=0;i<=maxu;i++)
    {
        if(mp[i].size()==k)
        {
            minu = min(minu,ans[i]);
        }
    }
    cout<<minu;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

/*** Template Begins ***/
#define ll          long long
#define pb          push_back
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define mi          map<int,int>
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define x           first
#define y           second
#define sz(x)       (int)x.size()
#define endl        '\n'
#define hell        1000000007
#define rep(i,a,b)  for(int i=a;i<b;i++)

// Debug //

#define trace(x)                 cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;

/*** Template Ends ***/

void solve(){
    ll n,k;
    cin>>n>>k;
    ll a[n];
    rep(i,0,n)cin>>a[i];
    sort(a,a+n);
    map<ll, vector<ll> > m;
    ll ans=1e15;
    rep(i,0,n)
    {
    	rep(j,0,20)
    	{
    		ll x=a[i]>>j;
    		if(m.find(x)==m.end())m[x].pb(j),m[x].pb(1);
    		else if(m[x][1]<k) m[x][0]=m[x][0]+j,m[x][1]=m[x][1]+1;
    		if(m[x][1]==k)ans=min(ans,m[x][0]);
    		trace3(x,m[x][0],m[x][1]);
    		if(x==0)break;
    	}
    }
    cout<<ans<<endl;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

/*** Template Begins ***/
#define ll          long long
#define pb          push_back
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define mi          map<int,int>
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define x           first
#define y           second
#define sz(x)       (int)x.size()
#define endl        '\n'
#define hell        1000000007
#define rep(i,a,b)  for(int i=a;i<b;i++)

// Debug //

#define trace(x)                 cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;

/*** Template Ends ***/

void solve(){
    ll n,k;
    cin>>n>>k;
    ll a[n];
    rep(i,0,n)cin>>a[i];
    sort(a,a+n);
    map<ll, pair<ll, ll> > m;
    ll ans=1e15;
    rep(i,0,n)
    {
    	rep(j,0,20)
    	{
    		ll x=a[i]>>j;
    		if(m.find(x)==m.end())m[x]={j,1};
    		else if(m[x].second<k) m[x]={m[x].first+j,m[x].second+1};
    		if(m[x].second==k)ans=min(ans,m[x].first);
    		trace3(x,m[x].first,m[x].second);
    		if(x==0)break;
    	}
    }
    cout<<ans<<endl;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

/*** Template Begins ***/
#define ll          int
#define pb          push_back
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define mi          map<int,int>
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define x           first
#define y           second
#define sz(x)       (int)x.size()
#define endl        '\n'
#define hell        1000000007
#define rep(i,a,b)  for(int i=a;i<b;i++)

// Debug //

#define trace(x)                 cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;

/*** Template Ends ***/

void solve(){
    ll n,k;
    cin>>n>>k;
    ll a[n];
    rep(i,0,n)cin>>a[i];
    sort(a,a+n);
    ll m[200005][2]={0};
    ll ans=INT_MAX;
    rep(i,0,n)
    {
    	ll x=a[i];
    	rep(j,0,20)
    	{

    		if(m[x][1]==0)m[x][0]=j,m[x][1]=1;
    		else if(m[x][1]<k) m[x][0]+=j,m[x][1]+=1;
    		if(m[x][1]==k)ans=min(ans,m[x][0]);
    		// trace3(x,m[x][0],m[x][1]);
    		if(x==0)break;
    		x/=2;
    	}
    }
    cout<<ans<<endl;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

/*** Template Begins ***/
#define ll          long long
#define pb          push_back
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define mi          map<int,int>
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define x           first
#define y           second
#define sz(x)       (int)x.size()
#define endl        '\n'
#define hell        1000000007
#define rep(i,a,b)  for(int i=a;i<b;i++)

// Debug //

#define trace(x)                 cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;

/*** Template Ends ***/

void solve(){
    ll n,k;
    cin>>n>>k;
    ll a[n];
    rep(i,0,n)cin>>a[i];
    sort(a,a+n);
    ll m[200005][2]={0};
    ll ans=1e15;
    rep(i,0,n)
    {
    	rep(j,0,20)
    	{
    		ll x=a[i]>>j;
    		if(m[x][1]==0)m[x][0]=j,m[x][1]=1;
    		else if(m[x][1]<k) m[x][0]+=j,m[x][1]+=1;
    		if(m[x][1]==k)ans=min(ans,m[x][0]);
    		trace3(x,m[x][0],m[x][1]);
    		if(x==0)break;
    	}
    }
    cout<<ans<<endl;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

/*** Template Begins ***/
#define ll          int
#define pb          push_back
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define mi          map<int,int>
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define x           first
#define y           second
#define sz(x)       (int)x.size()
#define endl        '\n'
#define hell        1000000007
#define rep(i,a,b)  for(int i=a;i<b;i++)

// Debug //

#define trace(x)                 cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;

/*** Template Ends ***/

void solve(){
    ll n,k;
    cin>>n>>k;
    ll a[n];
    rep(i,0,n)cin>>a[i];
    sort(a,a+n);
    ll m[200005][2]={0};
    ll ans=INT_MAX;
    rep(i,0,n)
    {
    	rep(j,0,20)
    	{
    		ll x=a[i]/pow(2,j);
    		if(m[x][1]==0)m[x][0]=j,m[x][1]=1;
    		else if(m[x][1]<k) m[x][0]+=j,m[x][1]+=1;
    		if(m[x][1]==k)ans=min(ans,m[x][0]);
    		trace3(x,m[x][0],m[x][1]);
    		if(x==0)break;
    	}
    }
    cout<<ans<<endl;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

/*** Template Begins ***/
#define ll          long long
#define pb          push_back
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define mi          map<int,int>
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define x           first
#define y           second
#define sz(x)       (int)x.size()
#define endl        '\n'
#define hell        1000000007
#define rep(i,a,b)  for(int i=a;i<b;i++)

// Debug //

#define trace(x)                 cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;

/*** Template Ends ***/

void solve(){
    ll n,k;
    cin>>n>>k;
    ll a[n];
    rep(i,0,n)cin>>a[i];
    sort(a,a+n);
    map<ll, pair<ll, ll> > m;
    ll ans=1e15;
    rep(i,0,n)
    {
    	rep(j,0,20)
    	{
    		ll x=a[i]>>j;
    		if(m.find(x)==m.end())m[x]={j,1};
    		else if(m[x].second<k) m[x]={m[x].first+j,m[x].second+1};
    		if(m[x].second==k)ans=min(ans,m[x].first);
    		trace3(x,m[x].first,m[x].second);
    	}
    }
    cout<<ans<<endl;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

/*** Template Begins ***/
#define ll          long long
#define pb          push_back
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define mi          map<int,int>
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define x           first
#define y           second
#define sz(x)       (int)x.size()
#define endl        '\n'
#define hell        1000000007
#define rep(i,a,b)  for(int i=a;i<b;i++)

// Debug //

#define trace(x)                 cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;

/*** Template Ends ***/

void solve(){
    ll n,k;
    cin>>n>>k;
    ll a[n];
    rep(i,0,n)cin>>a[i];
    sort(a,a+n);
    unordered_map<ll, pair<ll, ll> > m;
    ll ans=1e15;
    rep(i,0,n)
    {
    	rep(j,0,20)
    	{
    		ll x=a[i]>>j;
    		if(m.find(x)==m.end())m[x]={j,1};
    		else if(m[x].second<k) m[x]={m[x].first+j,m[x].second+1};
    		if(m[x].second==k)ans=min(ans,m[x].first);
    		trace3(x,m[x].first,m[x].second);
    		if(x==0)break;
    	}
    }
    cout<<ans<<endl;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

/*** Template Begins ***/
#define ll          int
#define pb          push_back
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define mi          map<int,int>
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define x           first
#define y           second
#define sz(x)       (int)x.size()
#define endl        '\n'
#define hell        1000000007
#define rep(i,a,b)  for(int i=a;i<b;i++)

// Debug //

#define trace(x)                 cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;

/*** Template Ends ***/

void solve(){
    ll n,k;
    cin>>n>>k;
    ll a[n];
    rep(i,0,n)cin>>a[i];
    sort(a,a+n);
    ll m[200005][2]={0};
    ll ans=INT_MAX;
    rep(i,0,n)
    {
    	ll x=a[i];
    	rep(j,0,20)
    	{

    		if(m[x][1]==0)m[x][0]=j,m[x][1]=1;
    		else if(m[x][1]<k) m[x][0]+=j,m[x][1]+=1;
    		if(m[x][1]==k)ans=min(ans,m[x][0]);
    		trace3(x,m[x][0],m[x][1]);
    		if(x==0)break;
    		x/=2;
    	}
    }
    cout<<ans<<endl;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}