#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
#define size 1000
int width;
int total=0;
typedef class OutPut
{
public:
int pos;
int code;
}output;
int InputPair[size][2];
output Out[size * 8];
int cmp(const void* a,const void* b)
{
output * x = (output *)a;
output * y = (output *)b;
return x->pos - y->pos;
}
int GetValue(int pos)
{
int i=0,p=0;
while( p < pos)
p += InputPair[i++][1];
return InputPair[i-1][0];
}
int GetCode(int pos)
{
int code = GetValue(pos);
int maxAbs = 0;
int row = (pos - 1) / width;
int col = (pos - 1) % width;
for(int i = row - 1;i <= row + 1;i++)
for(int j = col - 1;j <= col + 1;j++)
{
int tpos = i * width + j;
if(i<0 || j<0 || j >= width || tpos >= total || tpos==pos-1)
continue;
int tcode = GetValue(tpos + 1);
if(maxAbs < abs(tcode - code))
maxAbs = abs(tcode - code);
}
return maxAbs;
}
int main(int k)
{
while(cin>>width && width)
{
int pairv,pairt;
k=total=0;
while(cin>>pairv>>pairt && pairt)
{
InputPair[k][0]=pairv;
InputPair[k++][1]=pairt;
total += pairt;
}
int PairNum=k;
cout<<width<<endl;
int pos=1;
k=0;
for(int p=0;p<=PairNum;p++)
{
int row=(pos-1)/width;
int col=(pos-1)%width;
for(int i=row-1;i<=row+1;i++)
for(int j=col-1;j<=col+1;j++)
{
int tpos = i * width + j;
if(i<0 || j<0 || j >= width || tpos >= total)
continue;
Out[k].pos = tpos+1;
Out[k++].code = GetCode(tpos+1);
}
pos+=InputPair[p][1];
}
qsort(Out,k,sizeof(output),cmp);
output temp=Out[0];
for(int i=0;i<k;i++)
{
if(temp.code==Out[i].code)
continue;
cout<<temp.code<<' '<<Out[i].pos-temp.pos<<endl;
temp=Out[i];
}
cout<<temp.code<<' '<<total-temp.pos+1<<endl;
cout<<"0 0"<<endl;
}
cout<<0<<endl;
return 0;
}