#include <bits/stdc++.h>
#define ll long long int
#define itn int
#define endl "\n"
#define mOD  1000000007

 #define MAX 500005
 #include<iomanip>
 #define fast ios_base :: sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
 #define test int t; cin >> t; while(t--)
 #define inp(n) int n;cin >> n;
 #define mp make_pair
 #define pb push_back
 #define bsearch binary_search
 #define lbound lower_bound
 #define ubound upper_bound
 #define all(n) n.begin(), n.end()
 #define forl(i,a,b) for(ll i=a;i<b;i++)

 using namespace std;
 //int a[1000007];
 //vector <int> sievev;
 //void sieve()
 //{
 //    for(int i=0;i<=1000006;i++)
 //    a[i] = 1;
 //
 //    a[0]=a[1]=0;
 //    for(int i=2;i<=1000;i++)
 //    {
 //        if(a[i] == 1)
 //        {
 //            sievev.push_back(i);
 //            for(int j=2*i;j<=1000000;j=j+i)
 //            a[j] = 0;
 //        }
 //    }
 //    for(int i=1001;i<=1000000;i++)
 //        if(a[i]==1)
 //            sievev.push_back(i);
 //}
 //
 //template<class typ> class SegTree{int n,N; typ *arr,*tree; int built=0; public: SegTree(typ a[],int nn) { n = nn; arr = new typ[n]; arr = a; N = 100000; tree = new typ[N*2]; } SegTree(int nn){ n = nn; arr = new typ[n]; N = 100000; tree = new typ[N*2];} typ& operator[](int i){return arr[i];} void build(){ for (int i=0; i<n; i++) tree[n+i] = arr[i]; for (int i = n - 1; i > 0; --i) tree[i] = tree[i<<1] + tree[i<<1 | 1]; built=1;} void update(int p, typ value){if(!built) build(); tree[p+n] = value; p = p+n; for (int i=p; i > 1; i >>= 1) tree[i>>1] = tree[i] + tree[i^1];} typ query(int l, int r){ if(!built) build(); typ res = 0; for (l += n, r += n+1; l < r; l >>= 1, r >>= 1){ if (l&1) res += tree[l++]; if (r&1) res += tree[--r];} return res;}};
 //
 //int fpow(int x,int n)
 //{
 //    if(n==1)
 //        return x;
 //    if(n==0)
 //        return 1;
 //    if(n%2==0){
 //    int y = fpow(x,n/2);
 //    return (y*y);
 //    }
 //    return (x*fpow(x,n-1));
 //}
 //
 //int fpowmod(int x, int y, int p)
 //{
 //    int res = 1;
 //
 //    x = x % p;
 //
 //    while (y > 0)
 //    {
 //        if (y & 1)
 //            res = (res*x) % p;
 //
 //        y = y>>1;
 //        x = (x*x) % p;
 //    }
 //    return res;
 //}
 //
 //int modInv(int n, int p)
 //{
 //    return fpowmod(n, p-2, p);
 //}
 //
 //
 //void calcFac(int fac[], int n, int p)
 //{
 //    fac[0] = 1;
 //        for (int i=1 ; i<=n; i++)
 //            fac[i] = fac[i-1]*i%p;
 //}

 //int fac[MAX];
 //int power(int x, int y, int p)
 //{
 //    int res = 1;      // Initialize result
 //
 //    x = x % p;  // Update x if it is more than or
 //                // equal to p
 //
 //    while (y > 0)
 //    {
 //        // If y is odd, multiply x with result
 //        if (y & 1)
 //            res = (res*x) % p;
 //
 //        // y must be even now
 //        y = y>>1; // y = y/2
 //        x = (x*x) % p;
 //    }
 //    return res;
 //}

 // Returns n^(-1) mod p
 //int modInverse(int n, int p)
 //{
 //    return power(n, p-2, p);
 //}

 // Returns nCr % p using Fermat's little
 // theorem.
 //int nCrModPFermat(int n, int r, int p)
 //{
 //   // Base case
 //   if (r==0)
 //      return 1;
 //
 //    // Fill factorial array so that we
 //    // can find all factorial of r, n
 //    // and n-r
 //
 //
 //
 //    return (fac[n]* modInverse(fac[r], p) % p *
 //            modInverse(fac[n-r], p) % p) % p;

 //}

 //bool swa( aa e, aa eb)
 //{
 //    if(e.a<eb.a)return true;
 //    else return false;
 //
 //}
 //bool ss(aa r, aa s)
 //{
 //    if(r.b<s.b)return true;
 //    else return false;
 //
 //}

  ll mod(ll a,ll b)
{
    if(a>b)
        return a-b;
    else
        return b-a;
}
 void swap(ll* a,ll* b)
 {
     ll temp=*a;
     *a=*b;
     *b=temp;
 }

  // One by constructs remaining rows of Pascal
    ll power(ll x,ll y,ll p)
    {
       ll res = 1;      // Initialize result

        x = x % p;  // Update x if it is more than or
                    // equal to p

        while (y > 0)
        {
            // If y is odd, multiply x with result
            if (y & 1)
                res = (res*x) % p;

            // y must be even now
            y = y>>1; // y = y/2
            x = (x*x) % p;
        }
        return res;
    }

    // Returns n^(-1) mod p
 ll modInverse(ll n, ll p)
    {
        return power(n, p-2, p);
    }
          vector<ll> arr[8];
    // Returns nCr % p using Fermat's little
    // theorem.
 void dfs(ll a,vector<ll> ar[],bool vis[])
 {
    if(vis[a]==true)
    {}
     else
     {   cout<<a<<" ";
         vis[a]=true;
         for(auto it =ar[a].begin();it!=ar[a].end();it++)
         {
             dfs(*it,ar,vis);
         }
     }

 }
 void bfs(ll a,vector<ll> arr[])
 {
     bool vis[20];
     for(ll i=0;i<20;i++)
         vis[i]=false;
     list<ll>r;
     r.pb(a);
     vis[a]=true;
     while(!r.empty())
     {
         ll d = r.front();
         cout<<d<<" ";
         r.pop_front();
         for(auto it=arr[d].begin();it!=arr[d].end();it++)
         {
             if(vis[*it]==false)
             {vis[*it]=true;
             r.pb(*it);
             }
         }
     }
 }
 ll pos(ll a,ll b)
 {
     if(a>b)
     {
         return a-b;

     }
     else
         return b-a;
 }
 ll powerLL(ll x, ll n,ll MOD)
 {
     ll result = 1;
     while (n) {
         if (n & 1)
             result = result * x % MOD;
         n = n / 2;
         x = x * x % MOD;
     }
     return result;
 }
// array to store inverse of 1 to N
//ll N=200004;
//ll factorialNumInverse[300001 + 1];
//
//// array to precompute inverse of 1! to N!
//ll naturalNumInverse[300001 + 1];
//
//// array to store factorial of first N numbers
//ll fact[300001 + 1];
//
//// Function to precompute inverse of numbers
//void InverseofNumber(ll p)
//{
//    naturalNumInverse[0] = naturalNumInverse[1] = 1;
//    for (int i = 2; i <= N; i++)
//        naturalNumInverse[i] = naturalNumInverse[p % i] * (p - p / i) % p;
//}
//// Function to precompute inverse of factorials
//void InverseofFactorial(ll p)
//{
//    factorialNumInverse[0] = factorialNumInverse[1] = 1;
//
//    // precompute inverse of natural numbers
//    for (int i = 2; i <= N; i++)
//        factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p;
//}
//
//// Function to calculate factorial of 1 to N
//void factorial(ll p)
//{
//    fact[0] = 1;
//
//    // precompute factorials
//    for (int i = 1; i <= N; i++) {
//        fact[i] = (fact[i - 1] * i) % p;
//    }
//}

// Function to return nCr % p in O(1) time
//ll Binomial(ll N, ll R, ll p)
//{
//    // n C r = n!*inverse(r!)*inverse((n-r)!)
//    ll ans = ((fact[N] * factorialNumInverse[R])
//              % p * factorialNumInverse[N - R])
//             % p;
//    return ans;
//}
 int32_t main() {
     fast;
 //    fac[0] = 1;
 //    for (int i=1 ; i<=n; i++)
 //        fac[zi] = fac[i-1]*i%p;
 //
 //
     //test
//     {
         ll n,k;
         cin>>n>>k;
         ll arr[30005]={0};ll fre[30005]={0};vector<ll>aa[30004];
         for(ll i=0;i<n;i++)
         {
             cin>>arr[i];
             fre[arr[i]]++;
             ll d=arr[i];ll sk=0;
             while(d!=0)
             {
                 d=d/2;
                 sk++;
                 aa[d].pb(sk);
             }
         }
//         for(ll i=0;i<14;i++)
//
//         {
//             cout<<fre[i]<<"  "<<i<<endl;
//         }
         for(ll i=1;i<30004;i++)
         {
             sort(aa[i].begin(),aa[i].end());
         }
         ll ans=mOD;
         for(ll i=1;i<30005;i++)
         {
             ll j=k-fre[i];ll e=0;
             if(j<=0)
             {

                 ans=0;break;
             }
             if(aa[i].size()>=j)
             {

                 ll ccc=0;
                 while(j--)
                 {
                     e+=aa[i][ccc];
                     ccc++;
                 }
             }
             else
             {
                 continue;
             }
             ans=min(ans,e);
         }
         cout<<ans<<endl;
    // }
       return 0;
 }


#include <bits/stdc++.h>
#define ll long long int
#define itn int
#define endl "\n"
#define mOD  1000000007

 #define MAX 500005
 #include<iomanip>
 #define fast ios_base :: sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
 #define test int t; cin >> t; while(t--)
 #define inp(n) int n;cin >> n;
 #define mp make_pair
 #define pb push_back
 #define bsearch binary_search
 #define lbound lower_bound
 #define ubound upper_bound
 #define all(n) n.begin(), n.end()
 #define forl(i,a,b) for(ll i=a;i<b;i++)

 using namespace std;
 //int a[1000007];
 //vector <int> sievev;
 //void sieve()
 //{
 //    for(int i=0;i<=1000006;i++)
 //    a[i] = 1;
 //
 //    a[0]=a[1]=0;
 //    for(int i=2;i<=1000;i++)
 //    {
 //        if(a[i] == 1)
 //        {
 //            sievev.push_back(i);
 //            for(int j=2*i;j<=1000000;j=j+i)
 //            a[j] = 0;
 //        }
 //    }
 //    for(int i=1001;i<=1000000;i++)
 //        if(a[i]==1)
 //            sievev.push_back(i);
 //}
 //
 //template<class typ> class SegTree{int n,N; typ *arr,*tree; int built=0; public: SegTree(typ a[],int nn) { n = nn; arr = new typ[n]; arr = a; N = 100000; tree = new typ[N*2]; } SegTree(int nn){ n = nn; arr = new typ[n]; N = 100000; tree = new typ[N*2];} typ& operator[](int i){return arr[i];} void build(){ for (int i=0; i<n; i++) tree[n+i] = arr[i]; for (int i = n - 1; i > 0; --i) tree[i] = tree[i<<1] + tree[i<<1 | 1]; built=1;} void update(int p, typ value){if(!built) build(); tree[p+n] = value; p = p+n; for (int i=p; i > 1; i >>= 1) tree[i>>1] = tree[i] + tree[i^1];} typ query(int l, int r){ if(!built) build(); typ res = 0; for (l += n, r += n+1; l < r; l >>= 1, r >>= 1){ if (l&1) res += tree[l++]; if (r&1) res += tree[--r];} return res;}};
 //
 //int fpow(int x,int n)
 //{
 //    if(n==1)
 //        return x;
 //    if(n==0)
 //        return 1;
 //    if(n%2==0){
 //    int y = fpow(x,n/2);
 //    return (y*y);
 //    }
 //    return (x*fpow(x,n-1));
 //}
 //
 //int fpowmod(int x, int y, int p)
 //{
 //    int res = 1;
 //
 //    x = x % p;
 //
 //    while (y > 0)
 //    {
 //        if (y & 1)
 //            res = (res*x) % p;
 //
 //        y = y>>1;
 //        x = (x*x) % p;
 //    }
 //    return res;
 //}
 //
 //int modInv(int n, int p)
 //{
 //    return fpowmod(n, p-2, p);
 //}
 //
 //
 //void calcFac(int fac[], int n, int p)
 //{
 //    fac[0] = 1;
 //        for (int i=1 ; i<=n; i++)
 //            fac[i] = fac[i-1]*i%p;
 //}

 //int fac[MAX];
 //int power(int x, int y, int p)
 //{
 //    int res = 1;      // Initialize result
 //
 //    x = x % p;  // Update x if it is more than or
 //                // equal to p
 //
 //    while (y > 0)
 //    {
 //        // If y is odd, multiply x with result
 //        if (y & 1)
 //            res = (res*x) % p;
 //
 //        // y must be even now
 //        y = y>>1; // y = y/2
 //        x = (x*x) % p;
 //    }
 //    return res;
 //}

 // Returns n^(-1) mod p
 //int modInverse(int n, int p)
 //{
 //    return power(n, p-2, p);
 //}

 // Returns nCr % p using Fermat's little
 // theorem.
 //int nCrModPFermat(int n, int r, int p)
 //{
 //   // Base case
 //   if (r==0)
 //      return 1;
 //
 //    // Fill factorial array so that we
 //    // can find all factorial of r, n
 //    // and n-r
 //
 //
 //
 //    return (fac[n]* modInverse(fac[r], p) % p *
 //            modInverse(fac[n-r], p) % p) % p;

 //}

 //bool swa( aa e, aa eb)
 //{
 //    if(e.a<eb.a)return true;
 //    else return false;
 //
 //}
 //bool ss(aa r, aa s)
 //{
 //    if(r.b<s.b)return true;
 //    else return false;
 //
 //}

  ll mod(ll a,ll b)
{
    if(a>b)
        return a-b;
    else
        return b-a;
}
 void swap(ll* a,ll* b)
 {
     ll temp=*a;
     *a=*b;
     *b=temp;
 }

  // One by constructs remaining rows of Pascal
    ll power(ll x,ll y,ll p)
    {
       ll res = 1;      // Initialize result

        x = x % p;  // Update x if it is more than or
                    // equal to p

        while (y > 0)
        {
            // If y is odd, multiply x with result
            if (y & 1)
                res = (res*x) % p;

            // y must be even now
            y = y>>1; // y = y/2
            x = (x*x) % p;
        }
        return res;
    }

    // Returns n^(-1) mod p
 ll modInverse(ll n, ll p)
    {
        return power(n, p-2, p);
    }
          vector<ll> arr[8];
    // Returns nCr % p using Fermat's little
    // theorem.
 void dfs(ll a,vector<ll> ar[],bool vis[])
 {
    if(vis[a]==true)
    {}
     else
     {   cout<<a<<" ";
         vis[a]=true;
         for(auto it =ar[a].begin();it!=ar[a].end();it++)
         {
             dfs(*it,ar,vis);
         }
     }

 }
 void bfs(ll a,vector<ll> arr[])
 {
     bool vis[20];
     for(ll i=0;i<20;i++)
         vis[i]=false;
     list<ll>r;
     r.pb(a);
     vis[a]=true;
     while(!r.empty())
     {
         ll d = r.front();
         cout<<d<<" ";
         r.pop_front();
         for(auto it=arr[d].begin();it!=arr[d].end();it++)
         {
             if(vis[*it]==false)
             {vis[*it]=true;
             r.pb(*it);
             }
         }
     }
 }
 ll pos(ll a,ll b)
 {
     if(a>b)
     {
         return a-b;

     }
     else
         return b-a;
 }
 ll powerLL(ll x, ll n,ll MOD)
 {
     ll result = 1;
     while (n) {
         if (n & 1)
             result = result * x % MOD;
         n = n / 2;
         x = x * x % MOD;
     }
     return result;
 }
// array to store inverse of 1 to N
//ll N=200004;
//ll factorialNumInverse[300001 + 1];
//
//// array to precompute inverse of 1! to N!
//ll naturalNumInverse[300001 + 1];
//
//// array to store factorial of first N numbers
//ll fact[300001 + 1];
//
//// Function to precompute inverse of numbers
//void InverseofNumber(ll p)
//{
//    naturalNumInverse[0] = naturalNumInverse[1] = 1;
//    for (int i = 2; i <= N; i++)
//        naturalNumInverse[i] = naturalNumInverse[p % i] * (p - p / i) % p;
//}
//// Function to precompute inverse of factorials
//void InverseofFactorial(ll p)
//{
//    factorialNumInverse[0] = factorialNumInverse[1] = 1;
//
//    // precompute inverse of natural numbers
//    for (int i = 2; i <= N; i++)
//        factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p;
//}
//
//// Function to calculate factorial of 1 to N
//void factorial(ll p)
//{
//    fact[0] = 1;
//
//    // precompute factorials
//    for (int i = 1; i <= N; i++) {
//        fact[i] = (fact[i - 1] * i) % p;
//    }
//}

// Function to return nCr % p in O(1) time
//ll Binomial(ll N, ll R, ll p)
//{
//    // n C r = n!*inverse(r!)*inverse((n-r)!)
//    ll ans = ((fact[N] * factorialNumInverse[R])
//              % p * factorialNumInverse[N - R])
//             % p;
//    return ans;
//}
 int32_t main() {
     fast;
 //    fac[0] = 1;
 //    for (int i=1 ; i<=n; i++)
 //        fac[zi] = fac[i-1]*i%p;
 //
 //
//     test
//     {
         ll n,k;
         cin>>n>>k;
         ll arr[30005]={0};ll fre[30005]={0};vector<ll>aa[30004];
         for(ll i=0;i<n;i++)
         {
             cin>>arr[i];
             fre[arr[i]]++;
             ll d=arr[i];ll sk=0;
             while(d!=0)
             {
                 d=d/2;
                 sk++;
                 aa[d].pb(sk);
             }
         }
//         for(ll i=0;i<14;i++)
//
//         {
//             cout<<fre[i]<<"  "<<i<<endl;
//         }
         for(ll i=1;i<30004;i++)
         {
             sort(aa[i].begin(),aa[i].end());
         }
         ll ans=mOD;
         for(ll i=1;i<30005;i++)
         {
             ll j=k-fre[i];ll e=0;
             if(j<=0)
             {
                 cout<<k<<endl;
                 ans=0;break;
             }
             if(aa[i].size()>=j)
             {
                 ll cc=0;
                 while(j--)
                 {
                     e+=aa[i][cc];cc++;
                 }
             }
             else
             {
                 continue;
             }
             ans=min(ans,e);
         }
         cout<<ans<<endl;
//     }
       return 0;
 }


#include <bits/stdc++.h>
#define ll long long int
#define itn int
#define endl "\n"
#define mOD  1000000007

 #define MAX 500005
 #include<iomanip>
 #define fast ios_base :: sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
 #define test int t; cin >> t; while(t--)
 #define inp(n) int n;cin >> n;
 #define mp make_pair
 #define pb push_back
 #define bsearch binary_search
 #define lbound lower_bound
 #define ubound upper_bound
 #define all(n) n.begin(), n.end()
 #define forl(i,a,b) for(ll i=a;i<b;i++)

 using namespace std;
 //int a[1000007];
 //vector <int> sievev;
 //void sieve()
 //{
 //    for(int i=0;i<=1000006;i++)
 //    a[i] = 1;
 //
 //    a[0]=a[1]=0;
 //    for(int i=2;i<=1000;i++)
 //    {
 //        if(a[i] == 1)
 //        {
 //            sievev.push_back(i);
 //            for(int j=2*i;j<=1000000;j=j+i)
 //            a[j] = 0;
 //        }
 //    }
 //    for(int i=1001;i<=1000000;i++)
 //        if(a[i]==1)
 //            sievev.push_back(i);
 //}
 //
 //template<class typ> class SegTree{int n,N; typ *arr,*tree; int built=0; public: SegTree(typ a[],int nn) { n = nn; arr = new typ[n]; arr = a; N = 100000; tree = new typ[N*2]; } SegTree(int nn){ n = nn; arr = new typ[n]; N = 100000; tree = new typ[N*2];} typ& operator[](int i){return arr[i];} void build(){ for (int i=0; i<n; i++) tree[n+i] = arr[i]; for (int i = n - 1; i > 0; --i) tree[i] = tree[i<<1] + tree[i<<1 | 1]; built=1;} void update(int p, typ value){if(!built) build(); tree[p+n] = value; p = p+n; for (int i=p; i > 1; i >>= 1) tree[i>>1] = tree[i] + tree[i^1];} typ query(int l, int r){ if(!built) build(); typ res = 0; for (l += n, r += n+1; l < r; l >>= 1, r >>= 1){ if (l&1) res += tree[l++]; if (r&1) res += tree[--r];} return res;}};
 //
 //int fpow(int x,int n)
 //{
 //    if(n==1)
 //        return x;
 //    if(n==0)
 //        return 1;
 //    if(n%2==0){
 //    int y = fpow(x,n/2);
 //    return (y*y);
 //    }
 //    return (x*fpow(x,n-1));
 //}
 //
 //int fpowmod(int x, int y, int p)
 //{
 //    int res = 1;
 //
 //    x = x % p;
 //
 //    while (y > 0)
 //    {
 //        if (y & 1)
 //            res = (res*x) % p;
 //
 //        y = y>>1;
 //        x = (x*x) % p;
 //    }
 //    return res;
 //}
 //
 //int modInv(int n, int p)
 //{
 //    return fpowmod(n, p-2, p);
 //}
 //
 //
 //void calcFac(int fac[], int n, int p)
 //{
 //    fac[0] = 1;
 //        for (int i=1 ; i<=n; i++)
 //            fac[i] = fac[i-1]*i%p;
 //}

 //int fac[MAX];
 //int power(int x, int y, int p)
 //{
 //    int res = 1;      // Initialize result
 //
 //    x = x % p;  // Update x if it is more than or
 //                // equal to p
 //
 //    while (y > 0)
 //    {
 //        // If y is odd, multiply x with result
 //        if (y & 1)
 //            res = (res*x) % p;
 //
 //        // y must be even now
 //        y = y>>1; // y = y/2
 //        x = (x*x) % p;
 //    }
 //    return res;
 //}

 // Returns n^(-1) mod p
 //int modInverse(int n, int p)
 //{
 //    return power(n, p-2, p);
 //}

 // Returns nCr % p using Fermat's little
 // theorem.
 //int nCrModPFermat(int n, int r, int p)
 //{
 //   // Base case
 //   if (r==0)
 //      return 1;
 //
 //    // Fill factorial array so that we
 //    // can find all factorial of r, n
 //    // and n-r
 //
 //
 //
 //    return (fac[n]* modInverse(fac[r], p) % p *
 //            modInverse(fac[n-r], p) % p) % p;

 //}

 //bool swa( aa e, aa eb)
 //{
 //    if(e.a<eb.a)return true;
 //    else return false;
 //
 //}
 //bool ss(aa r, aa s)
 //{
 //    if(r.b<s.b)return true;
 //    else return false;
 //
 //}

  ll mod(ll a,ll b)
{
    if(a>b)
        return a-b;
    else
        return b-a;
}
 void swap(ll* a,ll* b)
 {
     ll temp=*a;
     *a=*b;
     *b=temp;
 }

  // One by constructs remaining rows of Pascal
    ll power(ll x,ll y,ll p)
    {
       ll res = 1;      // Initialize result

        x = x % p;  // Update x if it is more than or
                    // equal to p

        while (y > 0)
        {
            // If y is odd, multiply x with result
            if (y & 1)
                res = (res*x) % p;

            // y must be even now
            y = y>>1; // y = y/2
            x = (x*x) % p;
        }
        return res;
    }

    // Returns n^(-1) mod p
 ll modInverse(ll n, ll p)
    {
        return power(n, p-2, p);
    }
          vector<ll> arr[8];
    // Returns nCr % p using Fermat's little
    // theorem.
 void dfs(ll a,vector<ll> ar[],bool vis[])
 {
    if(vis[a]==true)
    {}
     else
     {   cout<<a<<" ";
         vis[a]=true;
         for(auto it =ar[a].begin();it!=ar[a].end();it++)
         {
             dfs(*it,ar,vis);
         }
     }

 }
 void bfs(ll a,vector<ll> arr[])
 {
     bool vis[20];
     for(ll i=0;i<20;i++)
         vis[i]=false;
     list<ll>r;
     r.pb(a);
     vis[a]=true;
     while(!r.empty())
     {
         ll d = r.front();
         cout<<d<<" ";
         r.pop_front();
         for(auto it=arr[d].begin();it!=arr[d].end();it++)
         {
             if(vis[*it]==false)
             {vis[*it]=true;
             r.pb(*it);
             }
         }
     }
 }
 ll pos(ll a,ll b)
 {
     if(a>b)
     {
         return a-b;

     }
     else
         return b-a;
 }
 ll powerLL(ll x, ll n,ll MOD)
 {
     ll result = 1;
     while (n) {
         if (n & 1)
             result = result * x % MOD;
         n = n / 2;
         x = x * x % MOD;
     }
     return result;
 }
// array to store inverse of 1 to N
//ll N=200004;
//ll factorialNumInverse[300001 + 1];
//
//// array to precompute inverse of 1! to N!
//ll naturalNumInverse[300001 + 1];
//
//// array to store factorial of first N numbers
//ll fact[300001 + 1];
//
//// Function to precompute inverse of numbers
//void InverseofNumber(ll p)
//{
//    naturalNumInverse[0] = naturalNumInverse[1] = 1;
//    for (int i = 2; i <= N; i++)
//        naturalNumInverse[i] = naturalNumInverse[p % i] * (p - p / i) % p;
//}
//// Function to precompute inverse of factorials
//void InverseofFactorial(ll p)
//{
//    factorialNumInverse[0] = factorialNumInverse[1] = 1;
//
//    // precompute inverse of natural numbers
//    for (int i = 2; i <= N; i++)
//        factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p;
//}
//
//// Function to calculate factorial of 1 to N
//void factorial(ll p)
//{
//    fact[0] = 1;
//
//    // precompute factorials
//    for (int i = 1; i <= N; i++) {
//        fact[i] = (fact[i - 1] * i) % p;
//    }
//}

// Function to return nCr % p in O(1) time
//ll Binomial(ll N, ll R, ll p)
//{
//    // n C r = n!*inverse(r!)*inverse((n-r)!)
//    ll ans = ((fact[N] * factorialNumInverse[R])
//              % p * factorialNumInverse[N - R])
//             % p;
//    return ans;
//}
 int32_t main() {
     fast;
 //    fac[0] = 1;
 //    for (int i=1 ; i<=n; i++)
 //        fac[zi] = fac[i-1]*i%p;
 //
 //
     //test
//     {
         ll n,k;
         cin>>n>>k;
         ll arr[300005]={0};ll fre[300005]={0};vector<ll>aa[300004];
         for(ll i=0;i<n;i++)
         {
             cin>>arr[i];
             fre[arr[i]]++;
             ll d=arr[i];ll sk=0;
             while(d!=0)
             {
                 d=d/2;
                 sk++;
                 aa[d].pb(sk);
             }
         }
//         for(ll i=0;i<14;i++)
//
//         {
//             cout<<fre[i]<<"  "<<i<<endl;
//         }
         for(ll i=1;i<300004;i++)
         {
             sort(aa[i].begin(),aa[i].end());
         }
         ll ans=mOD;
         for(ll i=1;i<300005;i++)
         {
             ll j=k-fre[i];ll e=0;
             if(j<=0)
             {

                 ans=0;break;
             }
             if(aa[i].size()>=j)
             {

                 ll ccc=0;
                 while(j--)
                 {
                     e+=aa[i][ccc];
                     ccc++;
                 }
             }
             else
             {
                 continue;
             }
             ans=min(ans,e);
         }
         cout<<ans<<endl;
    // }
       return 0;
 }


#include <bits/stdc++.h>
#define ll long long int
#define itn int
#define endl "\n"
#define mOD  1000000007

 #define MAX 500005
 #include<iomanip>
 #define fast ios_base :: sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
 #define test int t; cin >> t; while(t--)
 #define inp(n) int n;cin >> n;
 #define mp make_pair
 #define pb push_back
 #define bsearch binary_search
 #define lbound lower_bound
 #define ubound upper_bound
 #define all(n) n.begin(), n.end()
 #define forl(i,a,b) for(ll i=a;i<b;i++)

 using namespace std;
 //int a[1000007];
 //vector <int> sievev;
 //void sieve()
 //{
 //    for(int i=0;i<=1000006;i++)
 //    a[i] = 1;
 //
 //    a[0]=a[1]=0;
 //    for(int i=2;i<=1000;i++)
 //    {
 //        if(a[i] == 1)
 //        {
 //            sievev.push_back(i);
 //            for(int j=2*i;j<=1000000;j=j+i)
 //            a[j] = 0;
 //        }
 //    }
 //    for(int i=1001;i<=1000000;i++)
 //        if(a[i]==1)
 //            sievev.push_back(i);
 //}
 //
 //template<class typ> class SegTree{int n,N; typ *arr,*tree; int built=0; public: SegTree(typ a[],int nn) { n = nn; arr = new typ[n]; arr = a; N = 100000; tree = new typ[N*2]; } SegTree(int nn){ n = nn; arr = new typ[n]; N = 100000; tree = new typ[N*2];} typ& operator[](int i){return arr[i];} void build(){ for (int i=0; i<n; i++) tree[n+i] = arr[i]; for (int i = n - 1; i > 0; --i) tree[i] = tree[i<<1] + tree[i<<1 | 1]; built=1;} void update(int p, typ value){if(!built) build(); tree[p+n] = value; p = p+n; for (int i=p; i > 1; i >>= 1) tree[i>>1] = tree[i] + tree[i^1];} typ query(int l, int r){ if(!built) build(); typ res = 0; for (l += n, r += n+1; l < r; l >>= 1, r >>= 1){ if (l&1) res += tree[l++]; if (r&1) res += tree[--r];} return res;}};
 //
 //int fpow(int x,int n)
 //{
 //    if(n==1)
 //        return x;
 //    if(n==0)
 //        return 1;
 //    if(n%2==0){
 //    int y = fpow(x,n/2);
 //    return (y*y);
 //    }
 //    return (x*fpow(x,n-1));
 //}
 //
 //int fpowmod(int x, int y, int p)
 //{
 //    int res = 1;
 //
 //    x = x % p;
 //
 //    while (y > 0)
 //    {
 //        if (y & 1)
 //            res = (res*x) % p;
 //
 //        y = y>>1;
 //        x = (x*x) % p;
 //    }
 //    return res;
 //}
 //
 //int modInv(int n, int p)
 //{
 //    return fpowmod(n, p-2, p);
 //}
 //
 //
 //void calcFac(int fac[], int n, int p)
 //{
 //    fac[0] = 1;
 //        for (int i=1 ; i<=n; i++)
 //            fac[i] = fac[i-1]*i%p;
 //}

 //int fac[MAX];
 //int power(int x, int y, int p)
 //{
 //    int res = 1;      // Initialize result
 //
 //    x = x % p;  // Update x if it is more than or
 //                // equal to p
 //
 //    while (y > 0)
 //    {
 //        // If y is odd, multiply x with result
 //        if (y & 1)
 //            res = (res*x) % p;
 //
 //        // y must be even now
 //        y = y>>1; // y = y/2
 //        x = (x*x) % p;
 //    }
 //    return res;
 //}

 // Returns n^(-1) mod p
 //int modInverse(int n, int p)
 //{
 //    return power(n, p-2, p);
 //}

 // Returns nCr % p using Fermat's little
 // theorem.
 //int nCrModPFermat(int n, int r, int p)
 //{
 //   // Base case
 //   if (r==0)
 //      return 1;
 //
 //    // Fill factorial array so that we
 //    // can find all factorial of r, n
 //    // and n-r
 //
 //
 //
 //    return (fac[n]* modInverse(fac[r], p) % p *
 //            modInverse(fac[n-r], p) % p) % p;

 //}

 //bool swa( aa e, aa eb)
 //{
 //    if(e.a<eb.a)return true;
 //    else return false;
 //
 //}
 //bool ss(aa r, aa s)
 //{
 //    if(r.b<s.b)return true;
 //    else return false;
 //
 //}

  ll mod(ll a,ll b)
{
    if(a>b)
        return a-b;
    else
        return b-a;
}
 void swap(ll* a,ll* b)
 {
     ll temp=*a;
     *a=*b;
     *b=temp;
 }

  // One by constructs remaining rows of Pascal
    ll power(ll x,ll y,ll p)
    {
       ll res = 1;      // Initialize result

        x = x % p;  // Update x if it is more than or
                    // equal to p

        while (y > 0)
        {
            // If y is odd, multiply x with result
            if (y & 1)
                res = (res*x) % p;

            // y must be even now
            y = y>>1; // y = y/2
            x = (x*x) % p;
        }
        return res;
    }

    // Returns n^(-1) mod p
 ll modInverse(ll n, ll p)
    {
        return power(n, p-2, p);
    }
          vector<ll> arr[8];
    // Returns nCr % p using Fermat's little
    // theorem.
 void dfs(ll a,vector<ll> ar[],bool vis[])
 {
    if(vis[a]==true)
    {}
     else
     {   cout<<a<<" ";
         vis[a]=true;
         for(auto it =ar[a].begin();it!=ar[a].end();it++)
         {
             dfs(*it,ar,vis);
         }
     }

 }
 void bfs(ll a,vector<ll> arr[])
 {
     bool vis[20];
     for(ll i=0;i<20;i++)
         vis[i]=false;
     list<ll>r;
     r.pb(a);
     vis[a]=true;
     while(!r.empty())
     {
         ll d = r.front();
         cout<<d<<" ";
         r.pop_front();
         for(auto it=arr[d].begin();it!=arr[d].end();it++)
         {
             if(vis[*it]==false)
             {vis[*it]=true;
             r.pb(*it);
             }
         }
     }
 }
 ll pos(ll a,ll b)
 {
     if(a>b)
     {
         return a-b;

     }
     else
         return b-a;
 }
 ll powerLL(ll x, ll n,ll MOD)
 {
     ll result = 1;
     while (n) {
         if (n & 1)
             result = result * x % MOD;
         n = n / 2;
         x = x * x % MOD;
     }
     return result;
 }
// array to store inverse of 1 to N
//ll N=200004;
//ll factorialNumInverse[300001 + 1];
//
//// array to precompute inverse of 1! to N!
//ll naturalNumInverse[300001 + 1];
//
//// array to store factorial of first N numbers
//ll fact[300001 + 1];
//
//// Function to precompute inverse of numbers
//void InverseofNumber(ll p)
//{
//    naturalNumInverse[0] = naturalNumInverse[1] = 1;
//    for (int i = 2; i <= N; i++)
//        naturalNumInverse[i] = naturalNumInverse[p % i] * (p - p / i) % p;
//}
//// Function to precompute inverse of factorials
//void InverseofFactorial(ll p)
//{
//    factorialNumInverse[0] = factorialNumInverse[1] = 1;
//
//    // precompute inverse of natural numbers
//    for (int i = 2; i <= N; i++)
//        factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p;
//}
//
//// Function to calculate factorial of 1 to N
//void factorial(ll p)
//{
//    fact[0] = 1;
//
//    // precompute factorials
//    for (int i = 1; i <= N; i++) {
//        fact[i] = (fact[i - 1] * i) % p;
//    }
//}

// Function to return nCr % p in O(1) time
//ll Binomial(ll N, ll R, ll p)
//{
//    // n C r = n!*inverse(r!)*inverse((n-r)!)
//    ll ans = ((fact[N] * factorialNumInverse[R])
//              % p * factorialNumInverse[N - R])
//             % p;
//    return ans;
//}
 int32_t main() {
     fast;
 //    fac[0] = 1;
 //    for (int i=1 ; i<=n; i++)
 //        fac[zi] = fac[i-1]*i%p;
 //
 //
     //test
//     {
         ll n,k;
         cin>>n>>k;
         ll arr[30005]={0};ll fre[30005]={0};vector<ll>aa[30004];
         for(ll i=0;i<n;i++)
         {
             cin>>arr[i];
             fre[arr[i]]++;
             ll d=arr[i];ll sk=0;
             while(d!=0)
             {
                 d=d/2;
                 sk++;
                 aa[d].pb(sk);
             }
         }
//         for(ll i=0;i<14;i++)
//
//         {
//             cout<<fre[i]<<"  "<<i<<endl;
//         }
         for(ll i=1;i<30004;i++)
         {
             sort(aa[i].begin(),aa[i].end());
         }
         ll ans=mOD;
         for(ll i=1;i<30005;i++)
         {
             ll j=k-fre[i];ll e=0;
             if(j<=0)
             {

                 ans=0;break;
             }
             if(aa[i].size()>=j)
             {

                 while(j--)
                 {
                     e+=aa[i][j];
                 }
             }
             else
             {
                 continue;
             }
             ans=min(ans,e);
         }
         cout<<ans<<endl;
    // }
       return 0;
 }


#include <bits/stdc++.h>
#define ll long long int
#define itn int
#define endl "\n"
#define mOD  1000000007

 #define MAX 500005
 #include<iomanip>
 #define fast ios_base :: sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
 #define test int t; cin >> t; while(t--)
 #define inp(n) int n;cin >> n;
 #define mp make_pair
 #define pb push_back
 #define bsearch binary_search
 #define lbound lower_bound
 #define ubound upper_bound
 #define all(n) n.begin(), n.end()
 #define forl(i,a,b) for(ll i=a;i<b;i++)

 using namespace std;
 //int a[1000007];
 //vector <int> sievev;
 //void sieve()
 //{
 //    for(int i=0;i<=1000006;i++)
 //    a[i] = 1;
 //
 //    a[0]=a[1]=0;
 //    for(int i=2;i<=1000;i++)
 //    {
 //        if(a[i] == 1)
 //        {
 //            sievev.push_back(i);
 //            for(int j=2*i;j<=1000000;j=j+i)
 //            a[j] = 0;
 //        }
 //    }
 //    for(int i=1001;i<=1000000;i++)
 //        if(a[i]==1)
 //            sievev.push_back(i);
 //}
 //
 //template<class typ> class SegTree{int n,N; typ *arr,*tree; int built=0; public: SegTree(typ a[],int nn) { n = nn; arr = new typ[n]; arr = a; N = 100000; tree = new typ[N*2]; } SegTree(int nn){ n = nn; arr = new typ[n]; N = 100000; tree = new typ[N*2];} typ& operator[](int i){return arr[i];} void build(){ for (int i=0; i<n; i++) tree[n+i] = arr[i]; for (int i = n - 1; i > 0; --i) tree[i] = tree[i<<1] + tree[i<<1 | 1]; built=1;} void update(int p, typ value){if(!built) build(); tree[p+n] = value; p = p+n; for (int i=p; i > 1; i >>= 1) tree[i>>1] = tree[i] + tree[i^1];} typ query(int l, int r){ if(!built) build(); typ res = 0; for (l += n, r += n+1; l < r; l >>= 1, r >>= 1){ if (l&1) res += tree[l++]; if (r&1) res += tree[--r];} return res;}};
 //
 //int fpow(int x,int n)
 //{
 //    if(n==1)
 //        return x;
 //    if(n==0)
 //        return 1;
 //    if(n%2==0){
 //    int y = fpow(x,n/2);
 //    return (y*y);
 //    }
 //    return (x*fpow(x,n-1));
 //}
 //
 //int fpowmod(int x, int y, int p)
 //{
 //    int res = 1;
 //
 //    x = x % p;
 //
 //    while (y > 0)
 //    {
 //        if (y & 1)
 //            res = (res*x) % p;
 //
 //        y = y>>1;
 //        x = (x*x) % p;
 //    }
 //    return res;
 //}
 //
 //int modInv(int n, int p)
 //{
 //    return fpowmod(n, p-2, p);
 //}
 //
 //
 //void calcFac(int fac[], int n, int p)
 //{
 //    fac[0] = 1;
 //        for (int i=1 ; i<=n; i++)
 //            fac[i] = fac[i-1]*i%p;
 //}

 //int fac[MAX];
 //int power(int x, int y, int p)
 //{
 //    int res = 1;      // Initialize result
 //
 //    x = x % p;  // Update x if it is more than or
 //                // equal to p
 //
 //    while (y > 0)
 //    {
 //        // If y is odd, multiply x with result
 //        if (y & 1)
 //            res = (res*x) % p;
 //
 //        // y must be even now
 //        y = y>>1; // y = y/2
 //        x = (x*x) % p;
 //    }
 //    return res;
 //}

 // Returns n^(-1) mod p
 //int modInverse(int n, int p)
 //{
 //    return power(n, p-2, p);
 //}

 // Returns nCr % p using Fermat's little
 // theorem.
 //int nCrModPFermat(int n, int r, int p)
 //{
 //   // Base case
 //   if (r==0)
 //      return 1;
 //
 //    // Fill factorial array so that we
 //    // can find all factorial of r, n
 //    // and n-r
 //
 //
 //
 //    return (fac[n]* modInverse(fac[r], p) % p *
 //            modInverse(fac[n-r], p) % p) % p;

 //}

 //bool swa( aa e, aa eb)
 //{
 //    if(e.a<eb.a)return true;
 //    else return false;
 //
 //}
 //bool ss(aa r, aa s)
 //{
 //    if(r.b<s.b)return true;
 //    else return false;
 //
 //}

  ll mod(ll a,ll b)
{
    if(a>b)
        return a-b;
    else
        return b-a;
}
 void swap(ll* a,ll* b)
 {
     ll temp=*a;
     *a=*b;
     *b=temp;
 }

  // One by constructs remaining rows of Pascal
    ll power(ll x,ll y,ll p)
    {
       ll res = 1;      // Initialize result

        x = x % p;  // Update x if it is more than or
                    // equal to p

        while (y > 0)
        {
            // If y is odd, multiply x with result
            if (y & 1)
                res = (res*x) % p;

            // y must be even now
            y = y>>1; // y = y/2
            x = (x*x) % p;
        }
        return res;
    }

    // Returns n^(-1) mod p
 ll modInverse(ll n, ll p)
    {
        return power(n, p-2, p);
    }
          vector<ll> arr[8];
    // Returns nCr % p using Fermat's little
    // theorem.
 void dfs(ll a,vector<ll> ar[],bool vis[])
 {
    if(vis[a]==true)
    {}
     else
     {   cout<<a<<" ";
         vis[a]=true;
         for(auto it =ar[a].begin();it!=ar[a].end();it++)
         {
             dfs(*it,ar,vis);
         }
     }

 }
 void bfs(ll a,vector<ll> arr[])
 {
     bool vis[20];
     for(ll i=0;i<20;i++)
         vis[i]=false;
     list<ll>r;
     r.pb(a);
     vis[a]=true;
     while(!r.empty())
     {
         ll d = r.front();
         cout<<d<<" ";
         r.pop_front();
         for(auto it=arr[d].begin();it!=arr[d].end();it++)
         {
             if(vis[*it]==false)
             {vis[*it]=true;
             r.pb(*it);
             }
         }
     }
 }
 ll pos(ll a,ll b)
 {
     if(a>b)
     {
         return a-b;

     }
     else
         return b-a;
 }
 ll powerLL(ll x, ll n,ll MOD)
 {
     ll result = 1;
     while (n) {
         if (n & 1)
             result = result * x % MOD;
         n = n / 2;
         x = x * x % MOD;
     }
     return result;
 }
// array to store inverse of 1 to N
//ll N=200004;
//ll factorialNumInverse[300001 + 1];
//
//// array to precompute inverse of 1! to N!
//ll naturalNumInverse[300001 + 1];
//
//// array to store factorial of first N numbers
//ll fact[300001 + 1];
//
//// Function to precompute inverse of numbers
//void InverseofNumber(ll p)
//{
//    naturalNumInverse[0] = naturalNumInverse[1] = 1;
//    for (int i = 2; i <= N; i++)
//        naturalNumInverse[i] = naturalNumInverse[p % i] * (p - p / i) % p;
//}
//// Function to precompute inverse of factorials
//void InverseofFactorial(ll p)
//{
//    factorialNumInverse[0] = factorialNumInverse[1] = 1;
//
//    // precompute inverse of natural numbers
//    for (int i = 2; i <= N; i++)
//        factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p;
//}
//
//// Function to calculate factorial of 1 to N
//void factorial(ll p)
//{
//    fact[0] = 1;
//
//    // precompute factorials
//    for (int i = 1; i <= N; i++) {
//        fact[i] = (fact[i - 1] * i) % p;
//    }
//}

// Function to return nCr % p in O(1) time
//ll Binomial(ll N, ll R, ll p)
//{
//    // n C r = n!*inverse(r!)*inverse((n-r)!)
//    ll ans = ((fact[N] * factorialNumInverse[R])
//              % p * factorialNumInverse[N - R])
//             % p;
//    return ans;
//}
 int32_t main() {
     fast;
 //    fac[0] = 1;
 //    for (int i=1 ; i<=n; i++)
 //        fac[zi] = fac[i-1]*i%p;
 //
 //
     //test
//     {
         ll n,k;
         cin>>n>>k;
         ll arr[300005]={0};ll fre[300005]={0};vector<ll>aa[300009];
         for(ll i=0;i<n;i++)
         {
             cin>>arr[i];
             fre[arr[i]]++;
             ll d=arr[i];ll sk=0;
             while(d!=0)
             {
                 d=d/2;
                 sk++;
                 aa[d].pb(sk);
             }
         }
//         for(ll i=0;i<14;i++)
//
//         {
//             cout<<fre[i]<<"  "<<i<<endl;
//         }
         for(ll i=1;i<300004;i++)
         {
             sort(aa[i].begin(),aa[i].end());
         }
         ll ans=mOD;
         for(ll i=1;i<300005;i++)
         {
             ll j=k-fre[i];ll e=0;
             if(j<=0)
             {

                 ans=0;break;
             }
             if(aa[i].size()>=j)
             {

                 ll ccc=0;
                 while(j--)
                 {
                     e+=aa[i][ccc];
                     ccc++;
                 }
             }
             else
             {
                 continue;
             }
             ans=min(ans,e);
         }
         cout<<ans<<endl;
    // }
       return 0;
 }


#include <bits/stdc++.h>
#define ll long long int
#define itn int
#define endl "\n"
#define mOD  1000000007

 #define MAX 500005
 #include<iomanip>
 #define fast ios_base :: sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
 #define test int t; cin >> t; while(t--)
 #define inp(n) int n;cin >> n;
 #define mp make_pair
 #define pb push_back
 #define bsearch binary_search
 #define lbound lower_bound
 #define ubound upper_bound
 #define all(n) n.begin(), n.end()
 #define forl(i,a,b) for(ll i=a;i<b;i++)

 using namespace std;
 //int a[1000007];
 //vector <int> sievev;
 //void sieve()
 //{
 //    for(int i=0;i<=1000006;i++)
 //    a[i] = 1;
 //
 //    a[0]=a[1]=0;
 //    for(int i=2;i<=1000;i++)
 //    {
 //        if(a[i] == 1)
 //        {
 //            sievev.push_back(i);
 //            for(int j=2*i;j<=1000000;j=j+i)
 //            a[j] = 0;
 //        }
 //    }
 //    for(int i=1001;i<=1000000;i++)
 //        if(a[i]==1)
 //            sievev.push_back(i);
 //}
 //
 //template<class typ> class SegTree{int n,N; typ *arr,*tree; int built=0; public: SegTree(typ a[],int nn) { n = nn; arr = new typ[n]; arr = a; N = 100000; tree = new typ[N*2]; } SegTree(int nn){ n = nn; arr = new typ[n]; N = 100000; tree = new typ[N*2];} typ& operator[](int i){return arr[i];} void build(){ for (int i=0; i<n; i++) tree[n+i] = arr[i]; for (int i = n - 1; i > 0; --i) tree[i] = tree[i<<1] + tree[i<<1 | 1]; built=1;} void update(int p, typ value){if(!built) build(); tree[p+n] = value; p = p+n; for (int i=p; i > 1; i >>= 1) tree[i>>1] = tree[i] + tree[i^1];} typ query(int l, int r){ if(!built) build(); typ res = 0; for (l += n, r += n+1; l < r; l >>= 1, r >>= 1){ if (l&1) res += tree[l++]; if (r&1) res += tree[--r];} return res;}};
 //
 //int fpow(int x,int n)
 //{
 //    if(n==1)
 //        return x;
 //    if(n==0)
 //        return 1;
 //    if(n%2==0){
 //    int y = fpow(x,n/2);
 //    return (y*y);
 //    }
 //    return (x*fpow(x,n-1));
 //}
 //
 //int fpowmod(int x, int y, int p)
 //{
 //    int res = 1;
 //
 //    x = x % p;
 //
 //    while (y > 0)
 //    {
 //        if (y & 1)
 //            res = (res*x) % p;
 //
 //        y = y>>1;
 //        x = (x*x) % p;
 //    }
 //    return res;
 //}
 //
 //int modInv(int n, int p)
 //{
 //    return fpowmod(n, p-2, p);
 //}
 //
 //
 //void calcFac(int fac[], int n, int p)
 //{
 //    fac[0] = 1;
 //        for (int i=1 ; i<=n; i++)
 //            fac[i] = fac[i-1]*i%p;
 //}

 //int fac[MAX];
 //int power(int x, int y, int p)
 //{
 //    int res = 1;      // Initialize result
 //
 //    x = x % p;  // Update x if it is more than or
 //                // equal to p
 //
 //    while (y > 0)
 //    {
 //        // If y is odd, multiply x with result
 //        if (y & 1)
 //            res = (res*x) % p;
 //
 //        // y must be even now
 //        y = y>>1; // y = y/2
 //        x = (x*x) % p;
 //    }
 //    return res;
 //}

 // Returns n^(-1) mod p
 //int modInverse(int n, int p)
 //{
 //    return power(n, p-2, p);
 //}

 // Returns nCr % p using Fermat's little
 // theorem.
 //int nCrModPFermat(int n, int r, int p)
 //{
 //   // Base case
 //   if (r==0)
 //      return 1;
 //
 //    // Fill factorial array so that we
 //    // can find all factorial of r, n
 //    // and n-r
 //
 //
 //
 //    return (fac[n]* modInverse(fac[r], p) % p *
 //            modInverse(fac[n-r], p) % p) % p;

 //}

 //bool swa( aa e, aa eb)
 //{
 //    if(e.a<eb.a)return true;
 //    else return false;
 //
 //}
 //bool ss(aa r, aa s)
 //{
 //    if(r.b<s.b)return true;
 //    else return false;
 //
 //}

  ll mod(ll a,ll b)
{
    if(a>b)
        return a-b;
    else
        return b-a;
}
 void swap(ll* a,ll* b)
 {
     ll temp=*a;
     *a=*b;
     *b=temp;
 }

  // One by constructs remaining rows of Pascal
    ll power(ll x,ll y,ll p)
    {
       ll res = 1;      // Initialize result

        x = x % p;  // Update x if it is more than or
                    // equal to p

        while (y > 0)
        {
            // If y is odd, multiply x with result
            if (y & 1)
                res = (res*x) % p;

            // y must be even now
            y = y>>1; // y = y/2
            x = (x*x) % p;
        }
        return res;
    }

    // Returns n^(-1) mod p
 ll modInverse(ll n, ll p)
    {
        return power(n, p-2, p);
    }
          vector<ll> arr[8];
    // Returns nCr % p using Fermat's little
    // theorem.
 void dfs(ll a,vector<ll> ar[],bool vis[])
 {
    if(vis[a]==true)
    {}
     else
     {   cout<<a<<" ";
         vis[a]=true;
         for(auto it =ar[a].begin();it!=ar[a].end();it++)
         {
             dfs(*it,ar,vis);
         }
     }

 }
 void bfs(ll a,vector<ll> arr[])
 {
     bool vis[20];
     for(ll i=0;i<20;i++)
         vis[i]=false;
     list<ll>r;
     r.pb(a);
     vis[a]=true;
     while(!r.empty())
     {
         ll d = r.front();
         cout<<d<<" ";
         r.pop_front();
         for(auto it=arr[d].begin();it!=arr[d].end();it++)
         {
             if(vis[*it]==false)
             {vis[*it]=true;
             r.pb(*it);
             }
         }
     }
 }
 ll pos(ll a,ll b)
 {
     if(a>b)
     {
         return a-b;

     }
     else
         return b-a;
 }
 ll powerLL(ll x, ll n,ll MOD)
 {
     ll result = 1;
     while (n) {
         if (n & 1)
             result = result * x % MOD;
         n = n / 2;
         x = x * x % MOD;
     }
     return result;
 }
// array to store inverse of 1 to N
//ll N=200004;
//ll factorialNumInverse[300001 + 1];
//
//// array to precompute inverse of 1! to N!
//ll naturalNumInverse[300001 + 1];
//
//// array to store factorial of first N numbers
//ll fact[300001 + 1];
//
//// Function to precompute inverse of numbers
//void InverseofNumber(ll p)
//{
//    naturalNumInverse[0] = naturalNumInverse[1] = 1;
//    for (int i = 2; i <= N; i++)
//        naturalNumInverse[i] = naturalNumInverse[p % i] * (p - p / i) % p;
//}
//// Function to precompute inverse of factorials
//void InverseofFactorial(ll p)
//{
//    factorialNumInverse[0] = factorialNumInverse[1] = 1;
//
//    // precompute inverse of natural numbers
//    for (int i = 2; i <= N; i++)
//        factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p;
//}
//
//// Function to calculate factorial of 1 to N
//void factorial(ll p)
//{
//    fact[0] = 1;
//
//    // precompute factorials
//    for (int i = 1; i <= N; i++) {
//        fact[i] = (fact[i - 1] * i) % p;
//    }
//}

// Function to return nCr % p in O(1) time
//ll Binomial(ll N, ll R, ll p)
//{
//    // n C r = n!*inverse(r!)*inverse((n-r)!)
//    ll ans = ((fact[N] * factorialNumInverse[R])
//              % p * factorialNumInverse[N - R])
//             % p;
//    return ans;
//}
 int32_t main() {
     fast;
 //    fac[0] = 1;
 //    for (int i=1 ; i<=n; i++)
 //        fac[zi] = fac[i-1]*i%p;
 //
 //
     //test
//     {
         ll n,k;
         cin>>n>>k;
         ll arr[300005]={0};ll fre[300005]={0};vector<ll>aa[300004];
         for(ll i=0;i<n;i++)
         {
             cin>>arr[i];
             fre[arr[i]]++;
             ll d=arr[i];ll sk=0;
             while(d!=0)
             {
                 d=d/2;
                 sk++;
                 aa[d].pb(sk);
             }
         }
//         for(ll i=0;i<14;i++)
//
//         {
//             cout<<fre[i]<<"  "<<i<<endl;
//         }
         for(ll i=1;i<300004;i++)
         {
             sort(aa[i].begin(),aa[i].end());
         }
         ll ans=mOD;
         for(ll i=1;i<300005;i++)
         {
             ll j=k-fre[i];ll e=0;
             if(j<=0)
             {

                 ans=0;break;
             }
             if(aa[i].size()>=j)
             {
                 j--;
                 while(j--)
                 {
                     e+=aa[i][j];
                 }
             }
             else
             {
                 continue;
             }
             ans=min(ans,e);
         }
         cout<<ans<<endl;
    // }
       return 0;
 }


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
	ll n,k;
	cin>>n>>k;
	ll a[n];
	for(ll i=0;i<n;i++)cin>>a[i];
	set<ll> poss;
	poss.insert(0);
	for(ll i=0;i<n;i++)
	{
		ll x=a[i];
		while(x>0)
		{
			poss.insert(x);
			x/=2;
		}
	}
	ll ans=INT_MAX;
	for(auto i=poss.begin();i!=poss.end();i++)
	{
		ll y=*i;
		vector<ll> temp(n,INT_MAX);
		for(ll j=0;j<n;j++)
		{
			ll cnt=0;
			ll s=a[j];
			while(s>y)
			s/=2,cnt++;
			if(s==y)temp[j]=cnt;

		}
		sort(temp.begin(),temp.end());
		ll ans1=0;
		for(ll i=0;i<k;i++)ans1+=temp[i];
		ans=min(ans,ans1);
	}
	cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
//const double pi=acos(-1.0);
#define mod 1000000007
#define forn(i,k,n) for(ll i=k;i<n;i++)
#define fornr(i,k,k1) for(ll i=k;i>=k1;i--)
#define v(ll) vector<ll>
#define vl(a,n,ll,k) vector<ll> a(n,k)
#define vli(a,n) vector<ll> a(n)
#define init(a,n) vector<ll> a(n);forn(i,0,n)cin>>a[i]
#define vll(a,n,m,ll,k) vector<vector<ll>> a(n,vector<ll>(m,k))
#define mp(a,ll,ll1) map<ll,ll1> a
#define st(a,ll) set<ll> a
#define mpa make_pair
#define pr pair<ll,ll>
#define pb push_back
#define MAX 1000000
#define ed '\n'
#define fileTake ifstream cin("input.txt");ofstream cout("output.txt");
#define sortv(a) sort(a.begin(),a.end())
#define sorta(a,n) sort(a,a+n)
#define yeah "YES"
#define nil "NO"
#define All(x) x.begin(),x.end()
#define remDup(a) a.erase(unique(All(a)),a.end())
#define N 200005

void swap(ll &x,ll &y)
{
	x=x^y;
	y=x^y;
	x=x^y;
}

template<typename T>
void print(T a){for(auto i=a.begin();i!=a.end();i++)cout<<*i<<" ";cout<<endl;}
bool isVowel(char c)
{
	if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u')return true;
	return false;
}

void sync(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
v(ll) co[N];
int main(){
	sync();
	ll n,k;
	cin>>n>>k;
	init(a,n);
	forn(i,0,n)
	{
		ll o=a[i];
		ll p=0;
		while(o>=0)
		{
			co[o].pb(p);
			if(o==0)break;
			o/=2;
			p++;
		}
	}
	ll ans=LLONG_MAX;
	forn(i,0,N)
	{
		sortv(co[i]);
		if(co[i].size()>=k)
		{
			ll ans1=0;
			for(ll j=0;j<k;j++)ans1+=co[i][j];
			ans=max(ans,ans1);
		}
	}
	cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
//const double pi=acos(-1.0);
#define mod 1000000007
#define forn(i,k,n) for(ll i=k;i<n;i++)
#define fornr(i,k,k1) for(ll i=k;i>=k1;i--)
#define v(ll) vector<ll>
#define vl(a,n,ll,k) vector<ll> a(n,k)
#define vli(a,n) vector<ll> a(n)
#define init(a,n) vector<ll> a(n);forn(i,0,n)cin>>a[i]
#define vll(a,n,m,ll,k) vector<vector<ll>> a(n,vector<ll>(m,k))
#define mp(a,ll,ll1) map<ll,ll1> a
#define st(a,ll) set<ll> a
#define mpa make_pair
#define pr pair<ll,ll>
#define pb push_back
#define MAX 1000000
#define ed '\n'
#define fileTake ifstream cin("input.txt");ofstream cout("output.txt");
#define sortv(a) sort(a.begin(),a.end())
#define sorta(a,n) sort(a,a+n)
#define yeah "YES"
#define nil "NO"
#define All(x) x.begin(),x.end()
#define remDup(a) a.erase(unique(All(a)),a.end())
#define N 200005

void swap(ll &x,ll &y)
{
	x=x^y;
	y=x^y;
	x=x^y;
}

template<typename T>
void print(T a){for(auto i=a.begin();i!=a.end();i++)cout<<*i<<" ";cout<<endl;}
bool isVowel(char c)
{
	if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u')return true;
	return false;
}

void sync(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
v(ll) count[N];
int main(){
	sync();
	ll n,k;
	cin>>n>>k;
	init(a,n);
	forn(i,0,n)
	{
		ll o=a[i];
		ll p=0;
		while(o>=0)
		{
			count[o].pb(p);
			if(o==0)break;
			o/=2;
			p++;
		}
	}
	ll ans=LLONG_MAX;
	forn(i,0,N)
	{
		sortv(count[i]);
		if(count[i].size()>=k)
		{
			ll ans1=0;
			for(ll j=0;j<k;j++)ans1+=count[i][j];
			ans=max(ans,ans1);
		}
	}
	cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
//const double pi=acos(-1.0);
#define mod 1000000007
#define forn(i,k,n) for(ll i=k;i<n;i++)
#define fornr(i,k,k1) for(ll i=k;i>=k1;i--)
#define v(ll) vector<ll>
#define vl(a,n,ll,k) vector<ll> a(n,k)
#define vli(a,n) vector<ll> a(n)
#define init(a,n) vector<ll> a(n);forn(i,0,n)cin>>a[i]
#define vll(a,n,m,ll,k) vector<vector<ll>> a(n,vector<ll>(m,k))
#define mp(a,ll,ll1) map<ll,ll1> a
#define st(a,ll) set<ll> a
#define mpa make_pair
#define pr pair<ll,ll>
#define pb push_back
#define MAX 1000000
#define ed '\n'
#define fileTake ifstream cin("input.txt");ofstream cout("output.txt");
#define sortv(a) sort(a.begin(),a.end())
#define sorta(a,n) sort(a,a+n)
#define yeah "YES"
#define nil "NO"
#define All(x) x.begin(),x.end()
#define remDup(a) a.erase(unique(All(a)),a.end())
#define N 200005

void swap(ll &x,ll &y)
{
	x=x^y;
	y=x^y;
	x=x^y;
}

template<typename T>
void print(T a){for(auto i=a.begin();i!=a.end();i++)cout<<*i<<" ";cout<<endl;}
bool isVowel(char c)
{
	if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u')return true;
	return false;
}

void sync(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
v(ll) co[N];
int main(){
	sync();
	ll n,k;
	cin>>n>>k;
	init(a,n);
	forn(i,0,n)
	{
		ll o=a[i];
		ll p=0;
		while(o>=0)
		{
			co[o].pb(p);
			if(o==0)break;
			o/=2;
			p++;
		}
	}
	ll ans=LLONG_MAX;
	forn(i,0,N)
	{
		sortv(co[i]);
		if(co[i].size()>=k)
		{
			ll ans1=0;
			for(ll j=0;j<k;j++)ans1+=co[i][j];
			ans=min(ans,ans1);
		}
	}
	cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
	ll n,k;
	cin>>n>>k;
	ll a[n];
	for(ll i=0;i<n;i++)cin>>a[i];
	vector<vector<ll>> poss(200005);
	for(ll i=0;i<n;i++)
	{
		ll x=a[i];
		ll cnt=0;
		while(x>=0)
		{
			poss[x].push_back(cnt);
			cnt++;
			if(x==0)break;
			x/=2;
		}
	}
	ll ans=1e9;
	for(ll i=0;i<200005;i++)
	{
		sort(poss[i].begin(),poss[i].end());
		if(poss[i].size()>=k)
		{
			ll ans1=0;
			for(ll j=0;j<k;j++)ans1+=poss[i][j];
			ans=min(ans,ans1);
		}
	}
	cout<<ans<<endl;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <vector>
#include <functional>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <set>
#include <cstdlib>
#include <cassert>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <numeric>
#include <deque>
#include <random>
#include <chrono>
#include <ctime>
#include <climits>
#include <stack>
#include <array>
#include <iomanip>
#include <complex>
#include <bitset>
#include <iostream>
#include <cstring>

using namespace std;
using ll = long long;
using ld = double;
using ull = unsigned long long;

#define all(a) a.begin(), a.end()
#define len(a) (int)a.size()
#define pb push_back
#define eb emplace_back
#define em emplace
#define y0 withindarkness
#define y1 apinklotusbloomed
#define yn carryingapurplesoul
#define j1 togetherformingtheTeam

void useiostream() {
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cout.setf(ios::fixed), cout.precision(20);
}

void drop(char const* bu) {
	printf("%s", bu);
	exit(0);
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b) {
	return a / gcd(a, b) * b;
}

ld const pi = acos(-1.0L);
ld const eps = 0.0001L;

template<typename T, typename TT> bool mi(T& a, TT b) { return a > b ? (a = b, true) : false; }
template<typename T, typename TT> bool ma(T& a, TT b) { return a < b ? (a = b, true) : false; }

mt19937 ran(time(0));

int const mod = 1000000007;

int bp(int a, int p, int mod) {
	int re = 1;
	while (p) {
		if (p & 1)re = (re * 1LL * a) % mod;
		p >>= 1;
		a = (a * 1LL * a) % mod;
	}
	return re;
}

int dp[400005];
int cnt[400005];
int cnt2[400005];
int cc[400005][25];
int dep[400005];

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#else
	//freopen("points.in", "r", stdin);
	//freopen("points.out", "w", stdout);
#endif

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

	while (n--) {
		int a;
		cin >> a;
		cnt2[a]++;
		cnt[a]++;
	}

	for (int i = 200000; i > 0; i--) {
		int x = i / 2;
		cnt[x] += cnt[i];
		dp[x] += cnt[i] + dp[i];
	}
	int re = dp[0];
	queue<pair<int,int> > q;
	q.emplace(1,1);

	while (!q.empty()) {
		int v, d;
		tie(v, d) = q.front();
		q.pop();
		if (v > 200000)continue;
		dep[v] = d;
		q.emplace(v * 2, d + 1);
		q.emplace(v * 2 + 1, d + 1);
	}

	for (int i = 200000; i >= 0; i--) {
		int a = i * 2;
		for (int p = 1; p < 25; p++) {
			cc[i][p] += cc[a][p - 1];
		}
		a++;
		for (int p = 1; p < 25; p++) {
			cc[i][p] += cc[a][p - 1];
		}
		cc[i][0] += cnt[i];
	}

	for (int i = 200000; i >= 0; i--) {
		int kk = cnt[i];
		if (kk >= k) {
			int ans = dp[i];
			for (int j = 24; j >= 1; j--) {
				int todel;
				if (kk - cc[i][j] >= k)todel = cc[i][j];
				else todel = kk - k;
				ans -= todel * j;
				kk -= todel;
			}
			mi(re, ans);
		}
	}

	cout << re;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <vector>
#include <functional>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <set>
#include <cstdlib>
#include <cassert>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <numeric>
#include <deque>
#include <random>
#include <chrono>
#include <ctime>
#include <climits>
#include <stack>
#include <array>
#include <iomanip>
#include <complex>
#include <bitset>
#include <iostream>
#include <cstring>

using namespace std;
using ll = long long;
using ld = double;
using ull = unsigned long long;

#define all(a) a.begin(), a.end()
#define len(a) (int)a.size()
#define pb push_back
#define eb emplace_back
#define em emplace
#define y0 withindarkness
#define y1 apinklotusbloomed
#define yn carryingapurplesoul
#define j1 togetherformingtheTeam

void useiostream() {
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cout.setf(ios::fixed), cout.precision(20);
}

void drop(char const* bu) {
	printf("%s", bu);
	exit(0);
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b) {
	return a / gcd(a, b) * b;
}

ld const pi = acos(-1.0L);
ld const eps = 0.0001L;

template<typename T, typename TT> bool mi(T& a, TT b) { return a > b ? (a = b, true) : false; }
template<typename T, typename TT> bool ma(T& a, TT b) { return a < b ? (a = b, true) : false; }

mt19937 ran(time(0));

int const mod = 1000000007;

int bp(int a, int p, int mod) {
	int re = 1;
	while (p) {
		if (p & 1)re = (re * 1LL * a) % mod;
		p >>= 1;
		a = (a * 1LL * a) % mod;
	}
	return re;
}

int cnt[400005][25];

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#else
	//freopen("points.in", "r", stdin);
	//freopen("points.out", "w", stdout);
#endif

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

	while (n--) {
		int a;
		cin >> a;
		cnt[a][0] ++;
	}
	int re = INT_MAX;

	for (int i = 200000; i >= 0; i--) {
		for (int j = 24; j > 0; j--) {
			cnt[i][j] += cnt[i * 2][j - 1] + cnt[i * 2 + 1][j - 1];
		}
		int ans = 0;
		int cur = cnt[i][0];
		for (int j = 1; j < 25; j++) {
			if (cur + cnt[i][j] >= k) {
				ans += abs(cur - k) * j;
				cur = k;
				break;
			}
			else {
				ans += j*cnt[i][j];
				cur += cnt[i][j];
			}
		}
		if (cur >= k) {
			mi(re, ans);
		}
	}

	cout << re;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <vector>
#include <functional>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <set>
#include <cstdlib>
#include <cassert>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <numeric>
#include <deque>
#include <random>
#include <chrono>
#include <ctime>
#include <climits>
#include <stack>
#include <array>
#include <iomanip>
#include <complex>
#include <bitset>
#include <iostream>
#include <cstring>

using namespace std;
using ll = long long;
using ld = double;
using ull = unsigned long long;

#define all(a) a.begin(), a.end()
#define len(a) (int)a.size()
#define pb push_back
#define eb emplace_back
#define em emplace
#define y0 withindarkness
#define y1 apinklotusbloomed
#define yn carryingapurplesoul
#define j1 togetherformingtheTeam

void useiostream() {
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cout.setf(ios::fixed), cout.precision(20);
}

void drop(char const* bu) {
	printf("%s", bu);
	exit(0);
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b) {
	return a / gcd(a, b) * b;
}

ld const pi = acos(-1.0L);
ld const eps = 0.0001L;

template<typename T, typename TT> bool mi(T& a, TT b) { return a > b ? (a = b, true) : false; }
template<typename T, typename TT> bool ma(T& a, TT b) { return a < b ? (a = b, true) : false; }

mt19937 ran(time(0));

int const mod = 1000000007;

int bp(int a, int p, int mod) {
	int re = 1;
	while (p) {
		if (p & 1)re = (re * 1LL * a) % mod;
		p >>= 1;
		a = (a * 1LL * a) % mod;
	}
	return re;
}

int cnt[400005][25];

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#else
	//freopen("points.in", "r", stdin);
	//freopen("points.out", "w", stdout);
#endif

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

	while (n--) {
		int a;
		cin >> a;
		cnt[a][0] ++;
	}
	int re = INT_MAX;

	for (int i = 5; i >= 0; i--) {
		for (int j = 24; j > 0; j--) {
			cnt[i][j] += cnt[i * 2][j - 1] + cnt[i * 2 + 1][j - 1];
		}
		int ans = 0;
		int cur = cnt[i][0];
		for (int j = 1; j < 25; j++) {
			if (cur + cnt[i][j] >= k) {
				ans += abs(cur - k) * j;
				cur = k;
				break;
			}
			else {
				ans += j*cnt[i][j];
				cur += cnt[i][j];
			}
		}
		if (cur >= k) {
			mi(re, ans);
		}
	}

	cout << re;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <vector>
#include <functional>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <set>
#include <cstdlib>
#include <cassert>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <numeric>
#include <deque>
#include <random>
#include <chrono>
#include <ctime>
#include <climits>
#include <stack>
#include <array>
#include <iomanip>
#include <complex>
#include <bitset>
#include <iostream>
#include <cstring>

using namespace std;
using ll = long long;
using ld = double;
using ull = unsigned long long;

#define all(a) a.begin(), a.end()
#define len(a) (int)a.size()
#define pb push_back
#define eb emplace_back
#define em emplace
#define y0 withindarkness
#define y1 apinklotusbloomed
#define yn carryingapurplesoul
#define j1 togetherformingtheTeam

void useiostream() {
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cout.setf(ios::fixed), cout.precision(20);
}

void drop(char const* bu) {
	printf("%s", bu);
	exit(0);
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b) {
	return a / gcd(a, b) * b;
}

ld const pi = acos(-1.0L);
ld const eps = 0.0001L;

template<typename T, typename TT> bool mi(T& a, TT b) { return a > b ? (a = b, true) : false; }
template<typename T, typename TT> bool ma(T& a, TT b) { return a < b ? (a = b, true) : false; }

mt19937 ran(time(0));

int const mod = 1000000007;

int bp(int a, int p, int mod) {
	int re = 1;
	while (p) {
		if (p & 1)re = (re * 1LL * a) % mod;
		p >>= 1;
		a = (a * 1LL * a) % mod;
	}
	return re;
}

vector<int> g[200005];
int dp[200005];
int cnt[200005];

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#else
	//freopen("points.in", "r", stdin);
	//freopen("points.out", "w", stdout);
#endif

	for (int i = 1; i <= 200000; i++) {
		g[i].pb(i / 2);
	}

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

	while (n--) {
		int a;
		cin >> a;
		cnt[a]++;
	}

	int re = INT_MAX;

	for (int i = 200000; i >= 0; i--) {
		for (auto& x : g[i]) {
			cnt[x] += cnt[i];
			dp[x] += cnt[i] + dp[i];
		}
		if (cnt[i] >= k) {
			mi(re, dp[i] - abs(k - cnt[i]));
		}
	}

	cout << re;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <vector>
#include <functional>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <set>
#include <cstdlib>
#include <cassert>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <numeric>
#include <deque>
#include <random>
#include <chrono>
#include <ctime>
#include <climits>
#include <stack>
#include <array>
#include <iomanip>
#include <complex>
#include <bitset>
#include <iostream>
#include <cstring>

using namespace std;
using ll = long long;
using ld = double;
using ull = unsigned long long;

#define all(a) a.begin(), a.end()
#define len(a) (int)a.size()
#define pb push_back
#define eb emplace_back
#define em emplace
#define y0 withindarkness
#define y1 apinklotusbloomed
#define yn carryingapurplesoul
#define j1 togetherformingtheTeam

void useiostream() {
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cout.setf(ios::fixed), cout.precision(20);
}

void drop(char const* bu) {
	printf("%s", bu);
	exit(0);
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b) {
	return a / gcd(a, b) * b;
}

ld const pi = acos(-1.0L);
ld const eps = 0.0001L;

template<typename T, typename TT> bool mi(T& a, TT b) { return a > b ? (a = b, true) : false; }
template<typename T, typename TT> bool ma(T& a, TT b) { return a < b ? (a = b, true) : false; }

mt19937 ran(time(0));

int const mod = 1000000007;

int bp(int a, int p, int mod) {
	int re = 1;
	while (p) {
		if (p & 1)re = (re * 1LL * a) % mod;
		p >>= 1;
		a = (a * 1LL * a) % mod;
	}
	return re;
}

int cnt[400005][25];

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#else
	//freopen("points.in", "r", stdin);
	//freopen("points.out", "w", stdout);
#endif

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

	while (n--) {
		int a;
		cin >> a;
		cnt[a][0] ++;
	}
	int re = INT_MAX;

	for (int i = 200000; i >= 0; i--) {
		for (int j = 24; j > 0; j--) {
			cnt[i][j] += cnt[i * 2][j - 1] + cnt[i * 2 + 1][j - 1];
		}
		int ans = 0;
		int cur = cnt[i][0];
		for (int j = 1; j < 25; j++) {
			if (cur + cnt[i][j] >= k) {
				ans += abs(cur + cnt[i][j] - k) * j;
				cur = k;
				break;
			}
			else {
				ans += j*cnt[i][j];
				cur += cnt[i][j];
			}
		}
		if (cur >= k) {
			mi(re, ans);
		}
	}

	cout << re;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <vector>
#include <functional>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <set>
#include <cstdlib>
#include <cassert>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <numeric>
#include <deque>
#include <random>
#include <chrono>
#include <ctime>
#include <climits>
#include <stack>
#include <array>
#include <iomanip>
#include <complex>
#include <bitset>
#include <iostream>
#include <cstring>

using namespace std;
using ll = long long;
using ld = double;
using ull = unsigned long long;

#define all(a) a.begin(), a.end()
#define len(a) (int)a.size()
#define pb push_back
#define eb emplace_back
#define em emplace
#define y0 withindarkness
#define y1 apinklotusbloomed
#define yn carryingapurplesoul
#define j1 togetherformingtheTeam

void useiostream() {
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cout.setf(ios::fixed), cout.precision(20);
}

void drop(char const* bu) {
	printf("%s", bu);
	exit(0);
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b) {
	return a / gcd(a, b) * b;
}

ld const pi = acos(-1.0L);
ld const eps = 0.0001L;

template<typename T, typename TT> bool mi(T& a, TT b) { return a > b ? (a = b, true) : false; }
template<typename T, typename TT> bool ma(T& a, TT b) { return a < b ? (a = b, true) : false; }

mt19937 ran(time(0));

int const mod = 1000000007;

int bp(int a, int p, int mod) {
	int re = 1;
	while (p) {
		if (p & 1)re = (re * 1LL * a) % mod;
		p >>= 1;
		a = (a * 1LL * a) % mod;
	}
	return re;
}

int cnt[400005][25];

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#else
	//freopen("points.in", "r", stdin);
	//freopen("points.out", "w", stdout);
#endif

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

	while (n--) {
		int a;
		cin >> a;
		cnt[a][0]++;
	}
	int re = INT_MAX;

	for (int i = 200000; i >= 0; i--) {
		for (int j = 24; j > 0; j--) {
			if (i == 0)cnt[i][j] += cnt[1][j - 1];
			else
			cnt[i][j] += cnt[i * 2][j - 1] + cnt[i * 2 + 1][j - 1];
		}
		int ans = 0;
		int cur = cnt[i][0];
		for (int j = 1; j < 25; j++) {
			if (cur + cnt[i][j] >= k) {
				ans += abs(cur - k) * j;
				cur = k;
			}
			else {
				ans += j*cnt[i][j];
				cur += cnt[i][j];
			}
		}
		if (cur == k) {
			mi(re, ans);
		}
	}

	cout << re;
	return 0;
}


    #define _CRT_SECURE_NO_WARNINGS
    #include <cstdio>
    #include <vector>
    #include <functional>
    #include <string>
    #include <algorithm>
    #include <set>
    #include <map>
    #include <set>
    #include <cstdlib>
    #include <cassert>
    #include <unordered_map>
    #include <unordered_set>
    #include <queue>
    #include <numeric>
    #include <deque>
    #include <random>
    #include <chrono>
    #include <ctime>
    #include <climits>
    #include <stack>
    #include <array>
    #include <iomanip>
    #include <complex>
    #include <bitset>
    #include <iostream>
    #include <cstring>

    using namespace std;
    using ll = long long;
    using ld = double;
    using ull = unsigned long long;

    #define all(a) a.begin(), a.end()
    #define len(a) (int)a.size()
    #define pb push_back
    #define eb emplace_back
    #define em emplace
    #define y0 withindarkness
    #define y1 apinklotusbloomed
    #define yn carryingapurplesoul
    #define j1 togetherformingtheTeam

    void useiostream() {
    	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    	cout.setf(ios::fixed), cout.precision(20);
    }

    void drop(char const* bu) {
    	printf("%s", bu);
    	exit(0);
    }

    ll gcd(ll a, ll b) {
    	return b == 0 ? a : gcd(b, a % b);
    }

    ll lcm(ll a, ll b) {
    	return a / gcd(a, b) * b;
    }

    ld const pi = acos(-1.0L);
    ld const eps = 0.0001L;

    template<typename T, typename TT> bool mi(T& a, TT b) { return a > b ? (a = b, true) : false; }
    template<typename T, typename TT> bool ma(T& a, TT b) { return a < b ? (a = b, true) : false; }

    mt19937 ran(time(0));

    int const mod = 1000000007;

    int bp(int a, int p, int mod) {
    	int re = 1;
    	while (p) {
    		if (p & 1)re = (re * 1LL * a) % mod;
    		p >>= 1;
    		a = (a * 1LL * a) % mod;
    	}
    	return re;
    }

    int cnt[400005][25];

    int main() {
    #ifdef _DEBUG
    	freopen("input.txt", "r", stdin);
    	freopen("output.txt", "w", stdout);
    #else
    	//freopen("points.in", "r", stdin);
    	//freopen("points.out", "w", stdout);
    #endif

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

    	while (n--) {
    		int a;
    		cin >> a;
    		cnt[a][0]++;
    	}
    	int re = INT_MAX;

    	for (int i = 200000; i >= 0; i--) {
    	    if(cnt[i][0] >= k)re = 0;
    		for (int j = 24; j > 0; j--) {
    			if (i == 0)cnt[i][j] += cnt[1][j - 1];
    			else
    			cnt[i][j] += cnt[i * 2][j - 1] + cnt[i * 2 + 1][j - 1];
    		}
    		int ans = 0;
    		int cur = cnt[i][0];
    		for (int j = 1; j < 25; j++) {
    			if (cur + cnt[i][j] >= k) {
    				ans += abs(cur - k) * j;
    				cur = k;
    			}
    			else {
    				ans += j*cnt[i][j];
    				cur += cnt[i][j];
    			}
    		}
    		if (cur == k) {
    			mi(re, ans);
    		}
    	}

    	cout << re;
    	return 0;
    }


        #include <bits/stdc++.h>

        using namespace std;

        #define ll long long

        #define N (ll)1e6+5

        #define mod 1000000007
        #define inf mod

        ll n,m,x,y,z,t,mx,mn=inf,ans=inf;

        ll a[N];
        string str;
        vector<ll>v;

        int main(){
            cin >> n >> m;
            for(ll i=1;i<=n;i++)
                cin >> x,a[x]++;

            if(a[0] >= m){
                puts("0");
                return 0;
            }
            for(ll i=1;i<=200000;i++){
                if(a[i] >= m){
                    puts("0");
                    return 0;
                }
                a[i]+= a[i-1];
            }

            for(ll i=0; i <= 200000; i++){

                ll left;
                if(i!=0) left = m - a[i] + a[i-1];
                else left = m- a[i];
                ll cost=1;
                ll cur=0;
                ll bas = 2*i;
                ll son = 2*i+1;
                ll k;

                for(;bas<= 200000; bas*=2){
                    if(i!=0) k = a[bas-1];
                    else k = -1;

                    if(left <= a[son] - k ){
                        cur += left*cost;
                        ans = min(ans, cur);
                       // cout << cur << " " << i << endl;
                        break;
                    }

                    left -= a[son]-k;
                    cur += (a[son]-k)*cost;
                    cost++;
                    son = 2*son+1;

                }
            }
            cout << ans << endl;
        }


        #include <bits/stdc++.h>

        using namespace std;

        #define ll long long

        #define N (ll)1e6+5

        #define mod 1000000007
        #define inf mod

        ll n,m,x,y,z,t,mx,mn=inf,ans=inf;

        ll a[N];
        string str;
        vector<ll>v;

        int main(){
            cin >> n >> m;
            for(ll i=1;i<=n;i++)
                cin >> x,a[x]++;

            if(a[0] >= m){
                puts("0");
                return 0;
            }
            for(ll i=1;i<=200000;i++){
                if(a[i] >= m){
                    puts("0");
                    return 0;
                }
                a[i]+= a[i-1];
            }

            for(ll i=0; i <= 200000; i++){

                ll left;
                if(i!=0) left = m - a[i] + a[i-1];
                else left = m- a[i];
                ll cost=1;
                ll cur=0;
                ll bas = 2*i;
                ll son = 2*i+1;
                ll k;

                for(;bas<= 200000; bas*=2){
                    if(i!=0) k = a[bas-1];
                    else k = 1 ;

                    if(left <= a[son] - k ){
                        cur += left*cost;
                        ans = min(ans, cur);
                       // cout << cur << " " << i << endl;
                        break;
                    }

                    left -= a[son]-k;
                    cur += (a[son]-k)*cost;
                    cost++;
                    son = 2*son+1;

                }
            }
            cout << ans << endl;
        }


        #include <bits/stdc++.h>

        using namespace std;

        #define ll long long

        #define N (ll)1e6+5

        #define mod 1000000007
        #define inf mod

        ll n,m,x,y,z,t,mx,mn=inf,ans=inf;

        ll a[N];
        string str;
        vector<ll>v;

        int main(){
            cin >> n >> m;
            for(ll i=1;i<=n;i++)
                cin >> x,a[x]++;

            if(a[0] >= m){
                puts("0");
                return 0;
            }
            for(ll i=1;i<=200000;i++){
                if(a[i] >= m){
                    puts("0");
                    return 0;
                }
                a[i]+= a[i-1];
            }

            for(ll i=0; i <= 200000; i++){

                ll left;
                if(i!=0) left = m - a[i] + a[i-1];
                else left = m- a[i];
                ll cost=1;
                ll cur=0;
                ll bas = 2*i;
                ll son = 2*i+1;
                ll k;

                for(;bas<= 200000; bas*=2){
                    if(i!=0) k = a[bas-1];
                    else k =0;

                    if(left <= a[son] - k ){
                        cur += left*cost;
                        ans = min(ans, cur);
                       // cout << cur << " " << i << endl;
                        break;
                    }

                    left -= a[son]-k;
                    cur += (a[son]-k)*cost;
                    cost++;
                    son = 2*son+1;

                }
            }
            cout << ans << endl;
        }


    #include <bits/stdc++.h>

    using namespace std;

    #define ll long long

    #define N (int)1e6+5

    #define mod 1000000007
    #define inf mod

    ll n,m,x,y,z,t,mx,mn=inf,ans=inf;

    ll a[N];
    string str;
    vector<int>v;

    int main(){
        cin >> n >> m;
        for(int i=1;i<=n;i++)
            cin >> x,a[x]++;

        if(a[0] >= m){
            puts("0");
            return 0;
        }
        for(int i=1;i<=200000;i++){
            if(a[i] >= m){
                puts("0");
                return 0;
            }
            a[i]+= a[i-1];
        }

        for(int i=0; i <= 200000; i++){

            int left;
            if(i!=0) left = m - a[i] + a[i-1];
            else left = m- a[i];
            int cost=1;
            ll cur=0;
            int bas = 2*i;
            int son = 2*i+1;
            int k;
            if(i!=0) k = a[bas-1];
            else k =0;

            for(;bas<= 200000; bas*=2){

                if(left <= a[son] - k ){
                    cur += left*cost;
                    ans = min(ans, cur);
                   // cout << cur << " " << i << endl;
                    break;
                }

                left -= a[son]-k;
                cur += (a[son]-k)*cost;
                cost++;
                son = 2*son+1;

            }
        }
        cout << ans << endl;
    }


    #include <bits/stdc++.h>

    using namespace std;

    #define ll long long

    #define N (int)1e6+5

    #define mod 1000000007
    #define inf mod

    ll n,m,x,y,z,t,mx,mn=inf,ans=inf;

    ll a[N];
    string str;
    vector<int>v;

    int main(){
        cin >> n >> m;
        for(int i=1;i<=n;i++)
            cin >> x,a[x]++;

        if(a[0] >= m){
            puts("0");
            return 0;
        }
        for(int i=1;i<=200000;i++){
            if(a[i] >= m){
                puts("0");
                return 0;
            }
            a[i]+= a[i-1];
        }

        for(int i=0; i <= 200000; i++){

            int left;
            if(i!=0) left = m - a[i] + a[i-1];
            else left = m- a[i];
            int cost=1;
            ll cur=0;
            int bas = 2*i;
            int son = 2*i+1;

            for(;bas<= 200000; bas*=2){

                if(left <= a[son] - a[bas-1]){
                    cur += left*cost;
                    ans = min(ans, cur);
                   // cout << cur << " " << i << endl;
                    break;
                }

                left -= a[son]-a[bas-1];
                cur += (a[son]-a[bas-1])*cost;
                cost++;
                son = 2*son+1;

            }
        }
        cout << ans << endl;
    }


    #include <bits/stdc++.h>

    using namespace std;

    #define ll long long

    #define N (ll)1e6+5

    #define mod 1000000007
    #define inf mod

    ll n,m,x,y,z,t,mx,mn=inf,ans=inf;

    ll a[N];
    string str;
    vector<ll>v;

    int main(){
        cin >> n >> m;
        for(ll i=1;i<=n;i++)
            cin >> x,a[x]++;

        if(a[0] >= m){
            puts("0");
            return 0;
        }
        for(ll i=1;i<=200000;i++){
            if(a[i] >= m){
                puts("0");
                return 0;
            }
            a[i]+= a[i-1];
        }

        for(ll i=0; i <= 200000; i++){

            ll left;
            if(i!=0) left = m - a[i] + a[i-1];
            else left = m- a[i];
            ll cost=1;
            ll cur=0;
            ll bas = 2*i;
            ll son = 2*i+1;
            ll k;
            if(i!=0) k = a[bas-1];
            else k =0;

            for(;bas<= 200000; bas*=2){

                if(left <= a[son] - k ){
                    cur += left*cost;
                    ans = min(ans, cur);
                   // cout << cur << " " << i << endl;
                    break;
                }

                left -= a[son]-k;
                cur += (a[son]-k)*cost;
                cost++;
                son = 2*son+1;

            }
        }
        cout << ans << endl;
    }


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair < ll , ll > ii;
#define REP(i,a,b) for (int i = a; i <= b; i++)
#define PER(i,a,b) for (int i = a; i >= b; i--)
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define print(v) for (int iiiii=0;iiiii<v.size();iiiii++){cout<<#v<<"["<<iiiii<<"]: "<<v[iiiii]<<endl;}
#define printmap(v) {int i=0 ; for (auto kkk:v){cout<<#v<<"["<<kkk.first<<"]: "<<kkk.second<<endl;}}
#define prt(x) (cerr << #x << ": " << x<<endl)
#define printarr(v,siss) for (int iiiii=0;iiiii<siss;iiiii++){cout<<#v<<"["<<iiiii<<"]: "<<v[iiiii]<<endl;}
#define reset(a,b) for(int iiiii=0;iiiii<(*(&a + 1) - a);iiiii++)a[iiiii]=b;
#define sz(x) ((int)x.size())

#define p(A,B) (power(A,B))
inline int mul(int a, int b) {return (int) ((long long) a * b);}
inline int power(int a, long long b) {int res = 1;while (b > 0) {if (b & 1) {res = mul(res, a);}a = mul(a, a);b >>= 1;}return res;}
map<ll,ll> arr[2*100005];
int main(){
  //freopen("input.txt", "r", stdin);
  ios::sync_with_stdio(false);cin.tie(0);cout<<fixed;cout.precision(0);
  ll n,kk,ni;
  cin >> n >> kk;
  std::vector<ll> v;
  REP(i,0,n-1){
    cin >> ni;
    v.PB(ni);
  }
  for(auto k:v){
    ll counter=0;
    while(1){
      arr[k][counter]++;
      counter++;
      if(k==0)break;
      k/=2;
    }
  }
  ll minneed=LLONG_MAX;
  for(ll i=0;i<2*100005;i++){
    ll curnum=0;
    ll cursumm=0;
    ll need=kk;
    for(auto k:arr[i]){
      if(need>(k.second)){
        cursumm+=k.first*(k.second);
        need-=k.second;
      }else{
        cursumm+=need*k.first;
        need-=need;
      }
      if(need==0){
        if(minneed>cursumm){
          minneed=cursumm;

        }
      }
    }
  }
  cout << minneed << endl;

  return 0;
}


    #include <bits/stdc++.h>

    using namespace std;

    #define ll long long

    #define N (int)1e6+5

    #define mod 1000000007
    #define inf mod

    ll n,m,x,y,z,t,mx,mn=inf,ans=inf;

    ll a[N];
    string str;
    vector<int>v;

    int main(){
        cin >> n >> m;
        for(int i=1;i<=n;i++)
            cin >> x,a[x]++;

        if(a[0] >= m){
            puts("0");
            return 0;
        }
        for(int i=1;i<=200000;i++){
            if(a[i] >= m){
                puts("0");
                return 0;
            }
            a[i]+= a[i-1];
        }

        for(int i=0; i <= 200000; i++){

            int left = m - a[i] + a[i-1];

            int cost=1;
            ll cur=0;
            int bas = 2*i;
            int son = 2*i+1;

            for(;bas<= 200000; bas*=2){

                if(left <= a[son] - a[bas-1]){
                    cur += left*cost;
                    ans = min(ans, cur);
                   // cout << cur << " " << i << endl;
                    break;
                }

                left -= a[son]-a[bas-1];
                cur += (a[son]-a[bas-1])*cost;
                cost++;
                son = 2*son+1;

            }
        }
        cout << ans << endl;
    }


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi 2*acos(0.0)
#define f first
#define s second
#define pb push_back
#define gap ' '
#define mod 1000000007
#define INF 1000000000000000

ll sum[200010];

int main()
{
    int n,k;
    cin>>n>>k;
    int ara[n+5];
    set< pair<ll,int> > s[200010];

    for(int i=0;i<n;i++)
    {
        scanf("%d",&ara[i]);
        ll cnt=0;
        s[ara[i]].insert({cnt,i});
        while(ara[i]!=0)
        {
            ara[i]=ara[i]/2;
            cnt++;
            s[ara[i]].insert({cnt,i});
            sum[ara[i]]+=cnt;
            while(s[ara[i]].size()>k)
            {
                auto it=s[ara[i]].end();
                it--;
                sum[ara[i]]-=it->first;
                s[ara[i]].erase(it);
            }
        }
    }
    ll ans=100000000000000000;
    for(int i=0;i<=200000;i++)
    {
        if(s[i].size()>=k)
        {
            ans=min(ans,sum[i]);
        }
    }
    printf("%lld",ans);
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi 2*acos(0.0)
#define f first
#define s second
#define pb push_back
#define gap ' '
#define mod 1000000007
#define INF 1000000000000000

int sum[200005];

int main()
{
    int n,k;
    cin>>n>>k;
    int ara[n+5];
    set< pair<int,int> > s[200005];

    for(int i=0;i<n;i++)
    {
        cin>>ara[i];
        int cnt=0;
        s[ara[i]].insert({cnt,i});
        while(ara[i]!=0)
        {
            ara[i]=ara[i]/2;
            cnt++;
            if(ara[i]==0)
            {
                break;
            }
            s[ara[i]].insert({cnt,i});
            sum[ara[i]]+=cnt;
            while(s[ara[i]].size()>k)
            {
                auto it=s[ara[i]].end();
                it--;
                sum[ara[i]]-=it->first;
                s[ara[i]].erase(it);
            }
        }
    }
    int ans=1000000007;
    for(int i=1;i<=200000;i++)
    {
        if(s[i].size()>=k)
        {
            ans=min(ans,sum[i]);
        }
    }
    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi 2*acos(0.0)
#define f first
#define s second
#define pb push_back
#define gap ' '
#define mod 1000000007
#define INF 1000000000000000

ll sum[400010];

int main()
{
    int n,k;
    scanf("%d %d",&n,&k);
    int ara[n+5];
    set< pair<ll,int> > s[400005];

    for(int i=0;i<n;i++)
    {
        scanf("%d",&ara[i]);
        ll cnt=0;
        s[ara[i]].insert({cnt,i});
        while(ara[i]!=0)
        {
            ara[i]=ara[i]/2;
            cnt++;
            s[ara[i]].insert({cnt,i});
            sum[ara[i]]+=cnt;
            while(s[ara[i]].size()>k)
            {
                auto it=s[ara[i]].end();
                it--;
                sum[ara[i]]-=it->first;
                s[ara[i]].erase(it);
            }
        }
    }
    ll ans=100000000000000000;
    for(int i=0;i<=200000;i++)
    {
        if(s[i].size()>=k)
        {
            ans=min(ans,sum[i]);
        }
    }
    printf("%lld",ans);
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi 2*acos(0.0)
#define f first
#define s second
#define pb push_back
#define gap ' '
#define mod 1000000007
#define INF 1000000000000000

ll sum[400010];

int main()
{
    int n,k;
    scanf("%d %d",&n,&k);
    int ara[n+5];
    set< pair<ll,int> > s[400010];

    for(int i=0;i<n;i++)
    {
        scanf("%d",&ara[i]);
        ll cnt=0;
        while(ara[i]!=0)
        {
            s[ara[i]].insert({cnt,i});
            sum[ara[i]]+=cnt;
            while(s[ara[i]].size()>k)
            {
                auto it=s[ara[i]].end();
                it--;
                sum[ara[i]]-=it->first;
                s[ara[i]].erase(it);
            }
            ara[i]=ara[i]/2;
            cnt++;
        }
    }
    ll ans=100000000000000000;
    for(int i=1;i<=200000;i++)
    {
        if(s[i].size()>=k)
        {
            ans=min(ans,sum[i]);
        }
    }
    printf("%lld",ans);
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi 2*acos(0.0)
#define f first
#define s second
#define pb push_back
#define gap ' '
#define mod 1000000007
#define INF 1000000000000000

ll sum[200005];

int main()
{
    int n,k;
    cin>>n>>k;
    int ara[n+5];
    set< pair<ll,int> > s[200005];

    for(int i=0;i<n;i++)
    {
        scanf("%d",&ara[i]);
        ll cnt=0;
        s[ara[i]].insert({cnt,i});
        while(ara[i]!=0)
        {
            ara[i]=ara[i]/2;
            cnt++;
            if(ara[i]==0)
            {
                break;
            }
            s[ara[i]].insert({cnt,i});
            sum[ara[i]]+=cnt;
            while(s[ara[i]].size()>k)
            {
                auto it=s[ara[i]].end();
                it--;
                sum[ara[i]]-=it->first;
                s[ara[i]].erase(it);
            }
        }
    }
    ll ans=INF;
    for(int i=1;i<=200000;i++)
    {
        if(s[i].size()>=k)
        {
            ans=min(ans,sum[i]);
        }
    }
    printf("%lld",ans);
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi 2*acos(0.0)
#define f first
#define s second
#define pb push_back
#define gap ' '
#define mod 1000000007
#define INF 1000000000000000

ll sum[400010];

int main()
{
    int n,k;
    scanf("%d %d",&n,&k);
    int ara[n+5];
    set< pair<ll,int> > s[400005];

    for(int i=0;i<n;i++)
    {
        scanf("%d",&ara[i]);
        ll cnt=0;
        s[ara[i]].insert({cnt,i});
        while(ara[i]!=0)
        {
            ara[i]=ara[i]/2;
            cnt++;
            s[ara[i]].insert({cnt,i});
            sum[ara[i]]+=cnt;
            while(s[ara[i]].size()>k)
            {
                auto it=s[ara[i]].end();
                it--;
                sum[ara[i]]-=it->first;
                s[ara[i]].erase(it);
            }
        }
    }
    ll ans=100000000000000000;
    for(int i=0;i<=200000;i++)
    {
        if(s[i].size()==k)
        {
            ans=min(ans,sum[i]);
        }
    }
    printf("%lld",ans);
}


//Equalising by Division
#include<bits/stdc++.h>
using namespace std;
int largest;

int convertToZero(map<int,int> m ,int k)
{
	int steps=0;
	map<int,int> ::iterator it;
	for(it=m.begin();it!=m.end();it++)
	{
		int x = it->first;
		int s=0;
		while(x>0)
		{
			x = x/2;
			s++;
		}
		steps = steps + s*it->second;
	}

	return steps;
}
int check(int mid, map<int,int> m, int k)
{
	int eq = 0;
	if(m.count(mid))
		eq+=m[mid];
	int factor = 2;
	int curr = mid;
	int steps=0;
	while(curr<=largest && eq<k)
	{
		curr =  mid*factor;
		if(m.count(curr))
		{
			//cout<<"Checking for "<<mid<<" with "<<curr<<"\n";
			if(k-eq<=m[curr]){
				steps = steps + (k-eq)*(factor-1);
				eq=k;
				return steps;

			}

			else
			{
				eq+=m[curr];
				steps = steps + m[curr]*(factor-1);
			}

		}
		curr++;
		if(m.count(curr))
		{
			//cout<<"Checking for "<<mid<<" with "<<curr<<"\n";
			if(k-eq<=m[curr]){
				steps = steps + (k-eq)*(factor-1);
				eq=k;
				return steps;

			}

			else
			{
				eq+=m[curr];
				steps = steps + m[curr]*(factor-1);
			}
		}

		factor++;
	}
	if(eq<k)
		return -1;

	return steps;

}

int equaliseByDivision(int n, int k, int a[])
{
	//unsigned int low = 0, high = 2*pow(10,5);

	map<int,int> m;
	for(int i=0;i<n;i++)
	{
		m[a[i]]++;
		if(a[i]>largest)
			largest = a[i];
	}
	//cout<<largest<<"\n";
	int ans = INT_MAX;
	int mid = 1;
	while(mid<=largest)
	{
		//int mid = low + (high-low)/2;
		//Check if mid is the ans
		int temp = check(mid,m,k);
		if(temp!=-1)
		{
			ans = min(ans,temp);
		}

		mid++;
	}

	if(ans!=INT_MAX)
		return ans;

	return convertToZero(m,k);
}

int main()
{
	int n,k;
	cin>>n>>k;
	int a[n];
	for(int i=0;i<n;i++)
		cin>>a[i];
	largest = 0;
	cout<<equaliseByDivision(n,k,a);
	return 0;
}


//Equalising by Division
#include<bits/stdc++.h>
using namespace std;
int largest;

int convertToZero(map<int,int> m ,int k)
{
	int steps=0;
	vector<pair<int,int>> v;
	map<int,int> ::iterator it;
	for(it=m.begin();it!=m.end();it++)
	{
		v.push_back({it->first,it->second});

	}
	sort(v.begin(),v.end());
	int eq=0;
	for(int i=0;i<v.size();i++)
	{
		int x = v[i].first;
		int s = 0;
		while(x>0)
		{
			x = x/2;
			s++;
		}
		if(v[i].second>=(k-eq))
		{
			steps = steps + s*(k-eq);
			eq = k;
			return steps;
		}
		else
		{
			steps = steps + s*v[i].second;
			eq+=v[i].second;
		}
	}

	return steps;
}

int check(int mid, vector<pair<int,int>> v, int k, map<int,int> m)
{
	int eq = 0;
	if(m.count(mid))
		eq+=m[mid];
	if(eq>=k)
		return 0;
	int steps = 0;
	vector<int> dp(200011,-1);
	dp[mid] = 0;
	//vector<pair<int,int>> :: iterator it;
	//it = upper_bound(v.begin(),v.end(),make_pair(mid,0));
	int i;
	for( i=0;i<v.size();i++)
	{
		if(v[i].first<=mid)
			continue;

		int temp = v[i].first;
		int s=0;
		while(temp>mid)
		{
			if(dp[temp]!=-1)
			{
				s+=dp[temp];
				break;
			}
			s++;
			temp/=2;
		}

		if(temp==mid)
		{
			dp[v[i].first] =  s;
			if(v[i].second>=(k-eq))
			{
				steps = steps + s*(k-eq);
				return steps;
			}

			else{
				steps = steps + s*v[i].second;
				eq+=v[i].second;
			}
		}
	}

	if(eq>=k)
		return steps;

	return -1;

}

int equaliseByDivision(int n, int k, int a[])
{
	map<int,int> m;
	for(int i=0;i<n;i++)
	{
		m[a[i]]++;
		if(a[i]>largest)
			largest = a[i];
	}
	vector<pair<int,int>> v;
	map<int,int> ::iterator it;
	for(it=m.begin();it!=m.end();it++)
	{
		v.push_back({it->first,it->second});

	}
	sort(v.begin(),v.end());
	//cout<<largest<<"\n";
	int ans = convertToZero(m,k);		//let us say that all the numbers are being converted to zero
	//cout<<"Answer from convert to zero is: "<<ans<<"\n";
	int mid = 1;
	while(mid<=largest)
	{
		//int mid = low + (high-low)/2;
		//Check if mid is the ans
		int temp = check(mid,v,k,m);
		if(temp!=-1)
		{
			ans = min(ans,temp);
		}

		mid++;
	}

	return ans;

}

int main()
{
	int n,k;
	cin>>n>>k;
	int a[n];
	for(int i=0;i<n;i++)
		cin>>a[i];
	largest = 0;
	cout<<equaliseByDivision(n,k,a);
	return 0;
}


//Equalising by Division
#include<bits/stdc++.h>
using namespace std;
int largest;

int convertToZero(map<int,int> m ,int k)
{
	int steps=0;
	vector<pair<int,int>> v;
	map<int,int> ::iterator it;
	for(it=m.begin();it!=m.end();it++)
	{
		v.push_back({it->first,it->second});

	}
	sort(v.begin(),v.end());
	int eq=0;
	for(int i=0;i<v.size();i++)
	{
		int x = v[i].first;
		int s = 0;
		while(x>0)
		{
			x = x/2;
			s++;
		}
		if(v[i].second>=(k-eq))
		{
			steps = steps + s*(k-eq);
			eq = k;
			return steps;
		}
		else
		{
			steps = steps + s*v[i].second;
			eq+=v[i].second;
		}
	}

	return steps;
}

int check(int mid, vector<pair<int,int>> v, int k, map<int,int> m)
{
	int eq = 0;
	if(m.count(mid))
		eq+=m[mid];
	if(eq>=k)
		return 0;
	int steps = 0;
	map<int,int> dp;
	dp[mid] = 0;
	//vector<pair<int,int>> :: iterator it;
	//it = upper_bound(v.begin(),v.end(),make_pair(mid,0));
	int i;
	for( i=0;i<v.size();i++)
	{
		if(v[i].first<=mid)
			continue;

		int temp = v[i].first;
		int s=0;
		while(temp>mid)
		{
			if(dp.count(temp))
			{
				s+=dp[temp];
				break;
			}
			s++;
			temp/=2;
		}

		if(temp==mid)
		{
			dp[v[i].first] =  s;
			if(v[i].second>=(k-eq))
			{
				steps = steps + s*(k-eq);
				return steps;
			}

			else{
				steps = steps + s*v[i].second;
				eq+=v[i].second;
			}
		}
	}

	if(eq>=k)
		return steps;

	return -1;

}

int equaliseByDivision(int n, int k, int a[])
{
	map<int,int> m;
	for(int i=0;i<n;i++)
	{
		m[a[i]]++;
		if(a[i]>largest)
			largest = a[i];
	}
	vector<pair<int,int>> v;
	map<int,int> ::iterator it;
	for(it=m.begin();it!=m.end();it++)
	{
		v.push_back({it->first,it->second});

	}
	sort(v.begin(),v.end());
	//cout<<largest<<"\n";
	int ans = convertToZero(m,k);		//let us say that all the numbers are being converted to zero
	//cout<<"Answer from convert to zero is: "<<ans<<"\n";
	int mid = 1;
	while(mid<=largest)
	{
		//int mid = low + (high-low)/2;
		//Check if mid is the ans
		int temp = check(mid,v,k,m);
		if(temp!=-1)
		{
			ans = min(ans,temp);
		}

		mid++;
	}

	return ans;

}

int main()
{
	int n,k;
	cin>>n>>k;
	int a[n];
	for(int i=0;i<n;i++)
		cin>>a[i];
	largest = 0;
	cout<<equaliseByDivision(n,k,a);
	return 0;
}


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

int equaliseByDivision(int n,int k,int a[])
{
	vector<vector<int> > dp(200001);

	for(int i=0;i<n;i++)
	{
		int x = a[i];
		int s=0;
		while(x>1)
		{
			dp[x].push_back(s);
			x/=2;
			s++;
		}
	}

	int ans = INT_MAX;
	for(int i=0;i<200001;i++)
	{
		if(dp[i].size()<k)
			continue;

		sort(dp[i].begin(),dp[i].end());

		int temp=0;
		for(int j=0;j<k;j++)
		{
			temp+=dp[i][j];
		}
		ans = min(ans,temp);

	}

	return ans;
}

int main()
{
	int n,k;
	cin>>n>>k;
	int a[n];
	for(int i=0;i<n;i++)
		cin>>a[i];
	cout<<equaliseByDivision(n,k,a);
	return 0;
}


//Equalising by Division
#include<bits/stdc++.h>
using namespace std;
int largest;

int convertToZero(map<int,int> m ,int k)
{
	int steps=0;
	vector<pair<int,int>> v;
	map<int,int> ::iterator it;
	for(it=m.begin();it!=m.end();it++)
	{
		v.push_back({it->first,it->second});

	}
	sort(v.begin(),v.end());
	int eq=0;
	for(int i=0;i<v.size();i++)
	{
		int x = v[i].first;
		int s = 0;
		while(x>0)
		{
			x = x/2;
			s++;
		}
		if(v[i].second>=(k-eq))
		{
			steps = steps + s*(k-eq);
			eq = k;
			return steps;
		}
		else
		{
			steps = steps + s*v[i].second;
			eq+=v[i].second;
		}
	}

	return steps;
}

int check(int mid, vector<pair<int,int>> v, int k, map<int,int> m)
{
	int eq = 0;
	if(m.count(mid))
		eq+=m[mid];
	if(eq>=k)
		return 0;
	int steps = 0;
	for(int i=0;i<v.size();i++)
	{
		if(v[i].first<=mid)
			continue;

		int temp = v[i].first;
		int s=0;
		while(temp>mid)
		{
			s++;
			temp/=2;
		}

		if(temp==mid)
		{
			if(v[i].second>=(k-eq))
			{
				steps = steps + s*(k-eq);
				return steps;
			}

			else{
				steps = steps + s*v[i].second;
				eq+=v[i].second;
			}
		}
	}

	if(eq>=k)
		return steps;

	return -1;

}

int equaliseByDivision(int n, int k, int a[])
{
	map<int,int> m;
	for(int i=0;i<n;i++)
	{
		m[a[i]]++;
		if(a[i]>largest)
			largest = a[i];
	}
	vector<pair<int,int>> v;
	map<int,int> ::iterator it;
	for(it=m.begin();it!=m.end();it++)
	{
		v.push_back({it->first,it->second});

	}
	sort(v.begin(),v.end());
	//cout<<largest<<"\n";
	int ans = convertToZero(m,k);		//let us say that all the numbers are being converted to zero
	//cout<<"Answer from convert to zero is: "<<ans<<"\n";
	int mid = 1;
	while(mid<=largest)
	{
		//int mid = low + (high-low)/2;
		//Check if mid is the ans
		int temp = check(mid,v,k,m);
		if(temp!=-1)
		{
			ans = min(ans,temp);
		}

		mid++;
	}

	return ans;

}

int main()
{
	int n,k;
	cin>>n>>k;
	int a[n];
	for(int i=0;i<n;i++)
		cin>>a[i];
	largest = 0;
	cout<<equaliseByDivision(n,k,a);
	return 0;
}