#include <bits/stdc++.h>

using namespace std;

typedef long long i64;
typedef unsigned long long ui64;
typedef vector<i64> vi;
typedef vector<vi> vvi;
typedef pair<i64, i64> pi;

#define pb push_back
#define sz(a) i64((a).size())
#define all(c) (c).begin(), (c).end()
#define REP(s, e, i) for(i=(s); i < (e); ++i)
inline void RI(i64 &i) {scanf("%lld", &(i));}
inline void RVI(vi &v) { for(i64 i=0;i<sz(v);++i) { RI(v[i]); } }
inline void RVVI(vvi &vv) { for(i64 i=0;i<sz(vv);++i) { RVI(vv[i]); } }
inline void WI(const i64 &i) {printf("%lld\n", i);}
inline void WVI(const vi &v, char sep=' ') { for(i64 i=0;i<sz(v);++i) { if(i != 0){ printf("%c", sep); } printf("%lld", v[i]);} printf("\n"); }
inline void WS(const string &s) { printf("%s\n", s.c_str()); }
inline void WB(bool b, const string &yes, const string &no) { if(b){ WS(yes);} else { WS(no);} }
inline void YESNO(bool b) { WB(b, "YES", "NO"); }
inline void YesNo(bool b) { WB(b, "Yes", "No"); }
#define BUF_LENGTH 1000000
inline void RS(string &s) {static char buf[BUF_LENGTH]; scanf("%s", buf); s = buf;}
template<typename T> inline bool IN(T &S, const typename T::key_type &key) {
  return S.find(key) != S.end();
}
template<typename T> inline bool ON(const T &b, i64 idx) {
  return ((T(1) << idx) & b) != 0;
}

int main(int argc, char *argv[]) {
  i64 i, j, k;
  i64 N, M; RI(N); RI(M);
  vector<string> S(M);
  i64 L_min = 100, L_max = 0;
  REP(0, M, i) {
    RS(S[i]);
    L_min = min(L_min, sz(S[i]));
    L_max = max(L_max, sz(S[i]));
  }
  i64 L_avg = (L_min + L_max) / 2;
  // cerr << L_min << " " << L_max << endl;

  unordered_map<i64, vi> L;
  REP(0, M, i) {
    L[sz(S[i])].pb(i);
  }

  vector<string> ans(N, string(N, '.'));

  // horizontal
  i64 N_h = 0, lh;
  i64 Ch;
  REP(L_min, L_max+1, lh) {
    N_h += sz(L[lh]);
    Ch = N / lh; // how many on each row
    if(Ch * N <= N_h) {
      break;
    }
  }
  // cerr << Ch << " " << lh << endl;

  i64 l_cur = L_min;
  REP(0, N, i) {
    i64 pos = 0;
    REP(0, Ch, j) {
      i64 idx = L[l_cur].back();
      L[l_cur].pop_back();
      for(auto &c: S[idx]) {
        ans[i][pos] = c;
        ++pos;
      }
      if(L[l_cur].empty()) {
        L.erase(L.find(l_cur));
        ++l_cur;
      }
    }
    assert(l_cur <= lh);
    assert(pos <= Ch * lh);
  }

  i64 C_begin = Ch * lh;
  // vertical
  i64 N_c = 0, lc;
  i64 Cc;
  REP(l_cur, L_max+1, lc) {
    N_c += sz(L[lc]);
    Cc = N / lc; // how many on each column
    if(Cc * (N - C_begin) <= N_c) {
      break;
    }
  }
  //cerr << Cc << " " << lc << endl;

  REP(C_begin, N, i) {
    i64 pos = 0;
    REP(0, Cc, j) {
      i64 idx = L[l_cur].back();
      L[l_cur].pop_back();
      for(auto &c: S[idx]) {
        assert(ans[pos][i] == '.');
        ans[pos][i] = c;
        ++pos;
      }
      if(L[l_cur].empty()) {
        L.erase(L.find(l_cur));
        ++l_cur;
      }
    }
    assert(l_cur <= lc);
    assert(pos <= Cc * lc);
  }

  REP(0, N, i) {
    WS(ans[i]);
  }
 
  return 0;
}


//Bismillahir Rahmanir Raheem
#include<bits/stdc++.h>
typedef long long ll;
const ll INFLL = 1e18;
const ll INF =1e9;
using namespace std;
void fast()
{
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
}
ll n,a[300005],t,k;
int main()
{
    cin>>n>>k;
    for(int i=0;i<n;i++)
    {
        cin>>t;
        a[t]++;
    }
    ll ans=0;
    for(int i=0;i<=n;i++)
    {
        if(a[i]<k)
        {
            ans=ans+i*(k-a[i]);
            k=a[i];
        }
    }
    ans=ans+(n+1)*k;
    cout<<ans<<endl;
}
