#include<stdio.h>
#include<string.h>
#define fmax(b,c) (b)<(c)?(c):(b)
int a[100003][13];
int main()
{
int n;
while(scanf("%d",&n)&&n)
{
int b,c,i,j,k=0;
memset(a,0,sizeof(a));
for(i=0;i<n;i++)
{
scanf("%d%d",&b,&c);
a[c][b]++;
if(c>k) k=c;
}
for(i=k-1;i>=0;i--)
{
a[i][0]+=fmax(a[i+1][0],a[i+1][1]);
for(j=1;j<11;j++)
a[i][j]+=fmax(fmax(a[i+1][j-1],a[i+1][j]),a[i+1][j+1]);
}
printf("%d\n",a[0][5]);
//printf("%d\n",fmax(1,10));
}
return 0;
}
#include<string.h>
#define fmax(b,c) (b)<(c)?(c):(b)
int a[100003][13];
int main()
{
int n;
while(scanf("%d",&n)&&n)
{
int b,c,i,j,k=0;
memset(a,0,sizeof(a));
for(i=0;i<n;i++)
{
scanf("%d%d",&b,&c);
a[c][b]++;
if(c>k) k=c;
}
for(i=k-1;i>=0;i--)
{
a[i][0]+=fmax(a[i+1][0],a[i+1][1]);
for(j=1;j<11;j++)
a[i][j]+=fmax(fmax(a[i+1][j-1],a[i+1][j]),a[i+1][j+1]);
}
printf("%d\n",a[0][5]);
//printf("%d\n",fmax(1,10));
}
return 0;
}