#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long int ll;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for(auto it = d.b;it != d.e;++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, m;cin >> n >> m;
  set<int> s;
  vector<int> cnt(2000000);
  vector<int> arr(n);
  for(int i = 0;i < n;i++)cin >> arr[i];
  for(int i = 0;i <= n;i++)s.insert(i);
  for(int i = 0;i < m;i++){
    s.erase(arr[i]);
    cnt[arr[i]]++;
  }
  int ans = *s.begin();
  for(int i = m;i < n;i++){
    cnt[arr[i - m]]--;
    if(cnt[arr[i - m]] == 0)s.insert(arr[i - m]);
    cnt[arr[i]]++;
    s.erase(arr[i]);
    ans = min(ans, *s.begin());
  }
  cout << ans << endl;
}

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

bool comp(string& a, string& b) {
	return int(a.length()) < int(b.length());
}

bool check(char** ans,string &s, int i, int j, int n,unordered_map<char,vector<int>>& ii) {
	bool flg = true;

	for(int a=0;a<int(s.length());a++) {
		if (ans[(a+i)%n][j]!=s[a] && ans[(a+i)%n][j]!='.') {
			flg = false;
			break;
		} 
	}
	if (flg) {
		for(int a=0;a<int(s.length());a++) {
			ans[(a+i)%n][j] = s[a];
			vector<int> t = {(a+i)%n,j};
			ii[s[a]] = t;
		}
		return flg;
	}
	for(int a=0;a<int(s.length());a++) {
		if (ans[(i-a+n)%n][j]!=s[a] && ans[(i-a+n)%n][j]!='.') {
			flg = false;
			break;
		} 
	}
	if (flg) {
		for(int a=0;a<int(s.length());a++) {
			ans[(i-a+n)%n][j] = s[a];
			vector<int> t = {(i-a+n)%n,j};
			ii[s[a]] = t;
		}
		return flg;
	}

	for(int a=0;a<int(s.length());a++) {
		if (ans[i][(j+a)%n]!=s[a] &&ans[i][(j+a)%n]!='.') {
			flg = false;
			break;
		} 
	}
	if (flg) {
		for(int a=0;a<int(s.length());a++) {
			ans[i][(j+a)%n] = s[a];
			vector<int> t = {i,(j+a)%n};
			ii[s[a]] = t;
		}
		return flg;
	}
	for(int a=0;a<int(s.length());a++) {
		if (ans[i][(j-a+n)%n]!=s[a] &&ans[i][(j-a+n)%n]!='.') {
			flg = false;
			break;
		} 
	}
	if (flg) {
		for(int a=0;a<int(s.length());a++) {
			ans[i][(j-a+n)%n] = s[a];
			vector<int> t = {i,(j-a+n)%n};
			ii[s[a]] = t;
		}
	}
	return flg;
}

void solve(char** ans, vector<string>& s, int n) {
	unordered_map<char,vector<int>> ii;
	queue<vector<int>> que;
	for(int i=0;i<n;i++) {
		for(int j=0;j<n;j++) {
			vector<int> t = {i,j};
			que.push(t);
		}
	}

	for(int i=0;i<s.size();i++) {
		string& tar = s[i];
		auto itr = ii.find(tar[0]);

		if (itr!=ii.end()) {
			vector<int> kouho = ii[tar[0]];
			check(ans, tar, kouho[0], kouho[1], n,ii);
		}

		if (que.size()==0) continue;
		auto k = que.front();
		que.pop();
		check(ans, tar, k[0],k[1],n,ii);
	}

}

 
int main() {
	int n,m;
	cin >> n;
	cin >> m;
	vector<string> s;
	s.reserve(m);
	string t;
	for(int i=0;i<m;i++) {
		cin >> t;
		s.push_back(t);
	}
	sort(s.begin(),s.end(),comp);

	char **ans = new char*[n];
	for(int i=0;i<n;i++) {
		ans[i] = new char[n];
		for(int j=0;j<n;j++) {
			ans[i][j] = '.';
		}
	}
	solve(ans,s,n);
	for(int i=0;i<n;i++) {
		cout << ans[i] << endl;
	}

	return 0;
}