#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
#define _long 1010
struct out {
int val,loc;
}Out[9*_long];
int cmp(const void * x, const void * y){return ((out*)x) -> loc > ((out*)y) -> loc ? 1 : -1;}
int width,Inv[_long],Inn[_long],tot;
int getnum(int i,int j)
{
int k=i*width+j,t,sum=0;
for(t=0;sum-1<k;t++)
sum+=Inn[t];
return Inv[t-1];
}
int fun (int row,int col)
{
int p=getnum(row,col),i,j,m,Max=0;
for(i=row-1;i<=row+1;i++)
for(j=col-1;j<=col+1;j++){
if(i<0 || j<0 || i>=tot/width || j>=width || i==row && j==col) continue;
m=abs(getnum(i,j)-p);
Max=Max>m?Max:m;
}
return Max;
}
int main()
{
//freopen("1009.txt","r",stdin);
//freopen("out.txt","w",stdout);
int pairs,pos,i,j,cow,col,ans,posn;
while(cin>>width && width!=0)
{
cout<<width<<endl;
tot=0,pairs=0,pos=1,ans=0,posn=0;
while(cin>>Inv[pairs]>>Inn[pairs] && Inn[pairs]!=0)tot+=Inn[pairs++];
while(pos<=tot)
{
cow=(pos-1)/width,col=(pos-1)%width;
for(i=cow-1;i<=cow+1;i++)
for(j=col-1;j<=col+1;j++)
{
if(i<0 || j<0 || i>=tot/width || j>=width) continue;
Out[ans].val=fun(i,j),Out[ans++].loc=i*width+j;
}
pos+=Inn[posn++];
}
Out[ans].val=fun(tot/width-1,0),Out[ans++].loc=tot-width;
qsort(Out,ans,sizeof(Out[0]),cmp);
for(i=0;i<ans;)
{
cout<<Out[i].val<<" ";
for(j=i;Out[j].val==Out[i].val && j<ans;j++);
if(j==ans) cout<<tot-Out[i].loc<<endl;
else cout<<Out[j].loc-Out[i].loc<<endl;
i=j;
}
cout<<"0 0"<<endl;
}
cout<<"0";
return 0;
}