#include <iostream>
#include <vector>
#include <string>
using namespace std;

vector<vector<char>> mat;
vector<vector<char>> in;
int n,m;

void answer_print(){
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){        
            cout << mat[i][j] ;
            if(j==n-1) cout << endl;
        }
    }
}

bool isPut(int x,int y,vector<char> str){
    if(mat[y][x] != '.') return false;
    if(n-x+1 < str.size()) return false;
    return true;
}

void Put(int x,int y,vector<char> str){
    for(int i = x;i<x+str.size();i++){
        mat[y][i] = str[i-x];
    }
}

void solve(){
    int k=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(isPut(j,i,in[k])) {
                Put(j,i,in[k]);
                k++;
            }
        }
    }
}

int main(){

    // input
    cin >> n >> m;
    mat.resize(n);in.resize(m);
    for(int i=0;i<n;i++) mat[i].resize(n,'.');

    string tmp;
    for(int i=0;i<m;i++) {
        cin >> tmp;
        in[i].resize(tmp.size());
        for(int j=0;j<tmp.size();j++){
            in[i][j] = tmp[j];
        }
    }
    
    solve();
    
    answer_print();

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

#define ll long long
#define mod 1000000007
#define pll pair<long long,long long>
#define pdd pair<long double,long double>
#define vll vector<ll>
#define rep(i,j,n) for(int i=j;i<n;i++)
#define mp make_pair
#define pb push_back
#define pf push_front
#define inf 1e17
#define N 200010
priority_queue<pll,vector<pll>,greater<pll> >pq;
priority_queue<ll,vector<ll>,greater<ll> >pq1;

ll result(ll a, ll b, ll p){
    ll ans=1;
    if(b==(-1))
        b=p-2;
    while(b){
        if(b&1){
            ans=(ans*a)%p;
        }
        a=(a*a)%p;
        b>>=1;
    }
    return ans;
}

ll T = 1,n,m,k;

int main(){
    if(fopen("input.txt", "r"))
    freopen("input.txt", "r", stdin),
    freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    
    // cin >> T;
    while(T--){
        cin >> n >> k;
        ll a[n];
        map<ll, ll>m;
        for(int i=0;i<n;i++){
            cin >> a[i];
            m[a[i]]++;
        }
        ll ans = 0;
        ll d = 0;
        ll q = k;
        for(int i=0;i<n;i++){
            ll p = min(m[i], q);
            ans += (q-p)*d;
            d++;
            if(p==0 || q<=0)
                break;
            // cout << q << " " << p << "\n";
            q=p;
        }
        cout << ans <<"\n";
        
    }

    return 0;
}
