#include<iostream>
#include<algorithm>
using namespace std;
bool p[1000000];
int prime[80000];
int top;
int F(int p,int m){
	int left,right,midl,midr;
	left=0,right=top;
	while(true){
		if(left>=right-1)break;
		midl=(left+right)/2;
		if(prime[midl]<p-m)left=midl;
		else right=midl;
	}
	if(prime[midl]>=p-m)midl--;
	if(p-m<=2)midl=-1;
	left=0,right=top;
	while(true){
		if(left>=right-1)break;
		midr=(left+right)/2;
		if(prime[midr]==p+m)break;
		if(prime[midr]<p+m)left=midr;
		else right=midr;
	}
	//cout<<"%"<<prime[midl]<<" "<<prime[midr]<<endl;
	return midr-midl;
}
int main(){
	for(int i=0;i<1000000;i++)p[i]=true;
	for(int i=2;i<1000;i++)if(p[i])for(int j=i*i;j<1000000;j+=i)p[j]=false;
	top=0;
	for(int i=2;i<1000000;i++)if(p[i]){prime[top]=i;top++;}
	int n,p,m,ans;
	while(true){
	cin>>n;
	if(n==0)break;
	ans=0;
	for(int u=0;u<n;u++){
		cin>>p>>m;
		ans+=F(p,m)-1;
		//cout<<"#"<<F(p,m)<<endl;
	}
	cout<<ans<<endl;
	}
	return 0;
}