#include<algorithm>
#include<iostream>
#include<vector>
#include<string>
#include<queue>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template<typename T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return 0;}
template<typename T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return 0;}

using P = pair<ll, ll>;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    ll K;
    cin >> N >> K;
    vector<ll> A(N);
    for (auto& a : A) {
        cin >> a;
    }
    vector<ll> cnt(N + 1, 0);
    for (auto& a : A) {
        ++cnt[a];
    }
    priority_queue<P, vector<P>, greater<P>> que;
    rep(i, 0, N + 1) {
        que.emplace(cnt[i], i);
    }
    ll now_cnt = 0;
    ll max_a = N + 1;
    ll ans = 0;
    while (K) {
        while (!que.empty() && max_a < que.top().second) {
            que.pop();
        }
        if (que.empty()) {
            break;
        }
        P now = que.top();
        que.pop();
        ll diff = min(max(now.first - now_cnt, 0LL), K);
        ans += max_a * diff;
        now_cnt += diff;
        max_a = now.second;
        K -= diff;
    }
    cout << ans << endl;
    return 0;
}
#include "bits/stdc++.h"
using namespace std;
#define ll long long 
const int inf = 1e3+10;
const int row[]={1,-1,0,0},col[]={0,0,1,-1};
int main()
{
	int h,w;
	cin>>h>>w;
	string ab[h];
	int st=0,st1=0,gi,gj;
	vector <pair<int,int> > v[26];
	int alphavis[26];
	memset(alphavis,0,sizeof(alphavis));
	for (int i = 0; i < h; ++i)
	{
		cin>>ab[i];
		for (int j= 0; j<w ; ++j)
		{
			if(ab[i][j]=='S')
			{
				st=i,st1=j;
			}
			else if(ab[i][j]=='G')
			{
				gi=i;
				gj=j;
			}
			else if(ab[i][j]>='a'&&ab[i][j]<='z')
			{
				v[ab[i][j]-'a'].push_back({i,j});
			}
		}
	}
	queue<pair<int,int> >q;
	q.push({st,st1});
	int visit[h][w];
	memset(visit,-1,sizeof(visit));
	visit[st][st1]=0;
    while(!q.empty())
    {
        auto it=q.front();
        int r=it.first;
        int c=it.second;
        q.pop();
        for(int i=0;i<4;i++)
        {
        	int rr=r+row[i],cc=c+col[i];
        	if(rr>=0&&rr<h&&cc>=0&&cc<w&&ab[rr][cc]!='#')
        	{
        		if(visit[r][c]+1<visit[rr][cc]||visit[rr][cc]==-1)
        		{
           			if(ab[rr][cc]>='a'&&ab[rr][cc]<='z')
        			{
        				int sized=v[ab[rr][cc]-'a'].size();
        				if(alphavis[ab[rr][cc]-'a']==0){
        				for(int k=0;k<sized;k++)
        				{
        					int x=ab[rr][cc]-'a';
        					if(v[x][k].first==rr&&v[x][k].second==cc)				
        					visit[v[x][k].first][v[x][k].second]=visit[r][c]+1;
        					else 	
        					visit[v[x][k].first][v[x][k].second]=visit[r][c]+2;
        					q.push({v[x][k].first,v[x][k].second});
           				}
           				alphavis[ab[rr][cc]-'a']=1;
           			  }
           			else 
           				visit[rr][cc]=visit[r][c]+1;
        			}
        			else {
        			visit[rr][cc]=visit[r][c]+1;
        			q.push({rr,cc});
        		}
        		}
        	}
        }
    }
    cout<<visit[gi][gj]<<endl;
	return 0;
}