#include<iostream>
#include<cstring>
using namespace std;

bool prime[1000001];
int dp[1000001];

void seive(int s){
	for(int i=0;i<=s;i++)prime[i]=true;
	prime[0]=prime[1]=false;
	for(int i=2;i<=s;i++){
		if(prime[i]){
			for(int j=i*2;j<=s;j+=i)prime[j]=false;
		}
	}
}

void pp(int s){
	memset(dp,0,sizeof(dp));
	for(int i=1;i<=s;i++){
		dp[i]=dp[i-1]+prime[i];
	}
}

int main(void){
	seive(1000000);
	pp(1000000);
	int n;
	while(1){
		int ans=0;
		scanf("%d",&n);
		if(n==0)break;
		for(int i=0;i<n;i++){
			int p,m;
			scanf("%d%d",&p,&m);
			int ff=max(p-m,0);
			int tt=min(p+m,1000000);
			ans+=dp[tt]-dp[ff]+prime[ff];
		}
		printf("%d\n",ans-n);
	}
    return 0;
}