#include<bits/stdc++.h>
#define LL long long
#define GG long long
#define For(i, j, k) for(int i=j; i<=k; i++)
#define Dow(i, j, k) for(int i=j; i>=k; i--)
using namespace std;
inline GG read() {
GG x = 0, f = 1;
char ch = getchar();
while(ch<'0'||ch>'9') { if(ch=='-') f = -1; ch = getchar(); }
while(ch>='0'&&ch<='9') { x = x*10+ch-48; ch = getchar(); }
return x * f;
}
int n, G;
int P, Q;
string s[200];
int have[200], need[200];
int dp[100011][20];
int main() {
while(~scanf("%d",&n)&&n)
{
memset(dp, 0, sizeof(dp));
int res = 0;
int pos, ti;
For(i, 1, n) {
scanf("%d%d", &pos, &ti);
dp[ti][pos]++;
if(ti > res) res = ti;
}
Dow(i, res, 0)
For(j, 0, 10)
dp[i][j] += max(dp[i+1][j-1],max(dp[i+1][j+1],dp[i+1][j]));
printf("%d\n",dp[0][5]);
}
return 0;
}