#pragma GCC optimize ("O2")
#pragma GCC target ("avx")
//#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#include<iostream>
#include<cstring>
#include<bitset>


using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int CM = 1 << 17, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
	if (ci - owa > 0) {
		memcpy(cn, owa, CL);
		ci -= CM;
		fread(cn + CL, 1, CM, stdin);
	}
	ll tmp = *(ll*)ci;
	if ((tmp & ma0) ^ ma0) {
		int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
		tmp = tmp << dig & ma1;
		tmp = tmp * 10 + (tmp >> 8) & ma2;
		tmp = tmp * 100 + (tmp >> 16) & ma3;
		tmp = tmp * 10000 + (tmp >> 32) & ma4;
		ci += (72 - dig >> 3);
	}
	else {
		tmp = tmp & ma1;
		tmp = tmp * 10 + (tmp >> 8) & ma2;
		tmp = tmp * 100 + (tmp >> 16) & ma3;
		tmp = tmp * 10000 + (tmp >> 32) & ma4;
		ci += 8;
		if ((ct = *ci++) >= '0') {
			tmp = tmp * 10 + ct - '0';
			if ((ct = *ci++) >= '0') {
				tmp = tmp * 10 + ct - '0';
				ci++;
			}
		}
	}
	return tmp;
}
ll tmp[200001];
void pakuri_sort(int N, ll A[]) {
	const int b = 8;
	rep(k, 4) {
		int kazu[1 << b] = {}, kazu2[1 << b] = {};
		rep(i, N) kazu[A[i] >> k * b & ((1 << b) - 1)]++;
		rep(i, (1 << b) - 1) kazu[i + 1] += kazu[i];
		for (int i = N - 1; i >= 0; i--) tmp[--kazu[A[i] >> k * b & ((1 << b) - 1)]] = A[i];
		k++;
		rep(i, N) kazu2[tmp[i] >> k * b & ((1 << b) - 1)]++;
		rep(i, (1 << b) - 1) kazu2[i + 1] += kazu2[i];
		for (int i = N - 1; i >= 0; i--) A[--kazu2[tmp[i] >> k * b & ((1 << b) - 1)]] = tmp[i];
	}
}
bitset<400010> ST;
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);


	int N = getint(), M = getint();
	ll XY[200000];
	int k = 0;
	int nm1 = N - M, nm2 = N + M;
	rep(i, M) {
		int x = getint();
		int y = getint();
		if (y < nm1 | y > nm2) continue;
		XY[k++] = x | (ll)(y - N + 200001) << 32;
	}
	M = k;
	pakuri_sort(M, XY);

	ST[200001] = 1;
	const ll ma = (1ll << 32) - 1;
	int m = 0;
	while (m < M) {
		int m0 = m;
		int x = (XY[m] & ma);
		while (m < M && (XY[m] & ma) == x) {
			int y = (XY[m] >> 32);
			if (ST[y - 1] | ST[y + 1]) XY[m] |= 1ll;
			else if (ST[y]) XY[m] &= (~1ll);
			else XY[m] = 0;
			m++;
		}
		for (int p = m0; p < m; p++) {
			int y = (XY[p] >> 32);
			if (XY[p] & 1) ST[y] = 1;
			else if (XY[p]) ST[y] = 0;
		}
	}
	int kotae = ST.count();
	printf("%d\n", kotae);

	Would you please return 0;
}
#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
#include <random>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

int main()
{
    int N;
    int M;
    vector<string> S(M, "");

    int L = 0;

    cin >> N >> M;
    for (int i = 0; i < M; i++)
    {
        cin >> S.at(i);
        L = max(L, (int)S.at(i).size());
    }

    /*
    todo 親和性の高い部分列同士をあらかじめ結合しておく

    //結合度3以上の二つを結合する。
    for(int ketsugou = L; L > 2; L--){
        for(int i = 0; i)
    }
    */

    //ランダムにあてはめる。
    vector<vector<char>> board(20, vector<char>(20, '.'));
    //todo:Sを並べ替える
    //mt19937 get_rand_mt;
    //shuffle( S.begin(), S.end(), get_rand_mt );
    //sort(S.begin(), S.end(), [](string &a, string &b){return a.size() >= b.size();});

    for (string s : S)
    {
        int best_score = -1;
        int best_pos = -1;
        //横0縦1
        bool best_muki_is_tate = true;
        for (int i = 0; i < 400; i++)
        {
            int pos_i = i / 20;
            int pos_j = i % 20;
            int goodness = 0;
            for (int s_index = 0; s_index < (int)s.size(); s_index++)
            {
                if (board.at((pos_i + s_index) % 20).at(pos_j) == '.')
                {
                    continue;
                }
                else if (board.at((pos_i + s_index) % 20).at(pos_j) == s.at(s_index))
                {
                    goodness += 1;
                }
                else
                {
                    goodness = -1;
                    break;
                }
            }
            if (goodness > best_score)
            {
                best_score = goodness;
                best_pos = i;
                best_muki_is_tate = false;
            }
            goodness = 0;
            for (int s_index = 0; s_index < (int)s.size(); s_index++)
            {
                if (board.at(pos_i).at((pos_j + s_index) % 20) == '.')
                {
                    continue;
                }
                else if (board.at(pos_i).at((pos_j + s_index) % 20) == s.at(s_index))
                {
                    goodness += 1;
                }
                else
                {
                    goodness = -1;
                    break;
                }
            }
            if (goodness > best_score)
            {
                best_score = goodness;
                best_pos = i;
                best_muki_is_tate = true;
            }
        }
        //boardを更新する。
        if (best_score == -1)
        {
            continue;
        }
        int pos_i = best_pos / 20;
        int pos_j = best_pos % 20;
        for (int s_index = 0; s_index < (int)s.size(); s_index++)
        {
            board.at((pos_i + !best_muki_is_tate * s_index)%20).at((pos_j + best_muki_is_tate * s_index)%20) = s.at(s_index);
        }
    }
    for(int i = 0; i < 20; i++){
        for(int j = 0; j < 20; j++){
            cout << board.at(i).at(j);
        }
        cout << endl;
    }
    return 0;
}
