#include <algorithm>
#include <vector>
#include <cmath>
#include <cstdio>

using std::pair;
using std::make_pair;
using std::sort;

using std::vector;

struct RLE_image
{
	pair<int, int> st, ed;
	int color;
}in_image[1002];
int w, h;
int n;

unsigned char **board;

int n_row, n_col;
vector<int> t_row_num, t_col_num;

unsigned char **out;

FILE *fin = stdin;
FILE *fout = stdout;

void input()
{
	int t_color, t_repeat;
	int t_row, t_col;
	int foc=0;

	fscanf(fin, "%d", &w);

	if(w == 0)
		return;

	t_row = 0;
	t_col = 0;
	n = 0;

	do
	{
		fscanf(fin, "%d %d", &t_color, &t_repeat);

		if(t_color == 0 && t_repeat == 0)
			break;

		in_image[n].st = make_pair(t_row, t_col);
		t_col += (t_repeat - 1);
		t_row += (int)(t_col / w);
		t_col %= w;
		in_image[n].ed = make_pair(t_row, t_col);
		in_image[n].color = t_color;

		t_col++;
		if(t_col == w)
		{
			t_col = 0;
			t_row++;
		}
		n++;
	}while(1);

	h = t_row;
}


void make_board()
{
	int i;
	int *t_row, *t_col;

	t_row = new int[3*n+4];
	t_col = new int[3*n+4];

	for(i=0; i<n; i++)
	{
		if(0 < in_image[i].st.first) t_row[3*i+0] = in_image[i].st.first - 1;
		else t_row[3*i+0] = 0;

		t_row[3*i+1] = in_image[i].st.first;

		if(in_image[i].st.first < h) t_row[3*i+2] = in_image[i].st.first + 1;
		else t_row[3*i+2] = 0;
		

		if(0 < in_image[i].st.second) t_col[3*i+0] = in_image[i].st.second - 1;
		else t_col[3*i+0] = 0;

		t_col[3*i+1] = in_image[i].st.second;

		if(in_image[i].st.second < w) t_col[3*i+2] = in_image[i].st.second + 1;
		else t_col[3*i+2] = 0;		
	}

	t_row[3*n] = 1;
	t_row[3*n+1] = h-1;

	t_col[3*n] = 1;
	t_col[3*n+1] = w-1;

	sort(t_row, t_row + (3*n+2));
	sort(t_col, t_col + (3*n+2));

	t_row_num.clear();
	t_col_num.clear();
	n_row = 0;
	n_col = 0;

	t_row_num.push_back(1);

	for(i=0; i<3*n+2; i++)
	{
		if(t_row[i] == h)
			break;
		if(t_row_num[n_row] < t_row[i])
		{
			t_row_num.push_back(t_row[i]);
			n_row++;
		}
	}
	n_row++;
	if(h != 1)
	{
		t_row_num.push_back(h);
		n_row++;
	}
			
	t_col_num.push_back(1);
	for(i=0; i<3*n+2; i++)
	{
		if(t_col[i] == w)
			break;
		if(t_col_num[n_col] < t_col[i])
		{
			t_col_num.push_back(t_col[i]);
			n_col++;
		}
	}
	n_col++;
	if(w != 1)
	{
		t_col_num.push_back(w);
		n_col++;
	}
}

void make_board2()
{
	int foc = 0;
	int t_row, t_col;
	int i, j;

	board = new unsigned char*[n_row];
	out = new unsigned char*[n_row];
	
	for(i=0; i<n_row; i++)
	{
		board[i] = new unsigned char[n_col];
		out[i] = new unsigned char[n_col];
	}
		
	for(i=0; i<n_row; i++)
	{
		t_row = t_row_num[i];

		for(j=0; j<n_col; j++)
		{
			t_col = t_col_num[j];

			if(in_image[foc].ed.first < t_row - 1 
				|| (in_image[foc].ed.first == t_row - 1 && in_image[foc].ed.second < t_col - 1))
				foc++;
			
			board[i][j] = in_image[foc].color;
		}
	}
}

void process()
{
	int maxi;
	int ty[] = {-1, -1, -1, 0, 1, 1, 1, 0};
	int tx[] = {-1, 0, 1, 1, 1, 0, -1, -1};
	int yy, xx;

	for(int i=0; i<n_row; i++)
	{
		for(int j=0; j<n_col; j++)
		{
			maxi = 0;

			for(int k=0; k<8; k++)
			{
				yy = i + ty[k];
				xx = j + tx[k];
				
				if(yy < 0 || xx < 0 || n_row <= yy || n_col <= xx)
					continue;
				if(maxi < abs(board[i][j] - board[yy][xx]))
					maxi = abs(board[i][j] - board[yy][xx]);
			}

			out[i][j] = maxi;
		}
	}
}

void output()
{
	//fprintf(fout,"%d\n",w);
	int t_color, t_repeat;

	t_color = out[0][0];
	t_repeat = 0;

	for(int i=0; i<n_row; i++)
	{
		for(int j=0; j<n_col; j++)
		{
			if(t_color != out[i][j])
			{
				fprintf(fout, "%d %d\n", t_color, t_repeat);
				t_color = out[i][j];
				t_repeat = 0;
			}

			if(j == 0)
				t_repeat += t_col_num[j];
			else
				t_repeat += t_col_num[j] - t_col_num[j-1];
		}
		
		if(i != 0)
		{
			t_repeat += w * (t_row_num[i] - t_row_num[i-1] - 1);
		}
	}

	fprintf(fout, "%d %d\n0 0\n", t_color, t_repeat);

	delete[] board;
	delete[] out;
}

int main()
{
	input();
	make_board();
	make_board2();
	process();
	output();

	fclose(fin);
	fclose(fout);
	return 0;
}
