using namespace std;
int M=1000001,n,p,m,c,S,i,j;
bool P[1000001];
int main(){
	for(i=2;i<M;i++)if(!P[i])for(j=i+i;j<M;j+=i)P[j]=1;
	P[0]=P[1]=1;
	while(cin>>n,n){
		for(S=0;n--;){
			cin>>p>>m;
			c=0; for(i=p-m>0?p-m:0;i<=p+m&&i<M;)if(!P[i++])c++;
			S+=c>0?c-1:-1;
		}
		cout<<S<<endl;
	}
	return 0;
}