#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
bool prime[1000001];
vector<int> ppp;

void p_init(){
	prime[0] = prime[1] = true;
	for(int i=0;i<=1000;i++){
		if(!prime[i])
			for(int j=i<<1;j<=1000000;j+=i){
				prime[j] = true;
		}
	}

	for(int i=0;i<=1000000;i++){
		if(!prime[i]){
			ppp.push_back(i);
		}
	}
}

int main(){
	p_init();
	int x,n,p,m;
	int ret;
	while(cin >> n && n){
		ret = 0;
		for(int i=0;i<n;i++){
			scanf("%d %d",&p,&m);
			int mi = p<m ? 0 : p-m;
			int mx = p+m>1000000?1000000:p+m;
			x = 0;
			for(int i=0;i<ppp.size();i++){
				if(ppp[i]>mx)break;
				if(ppp[i]>=mi){
					ret++;
				}
			}
			if(x){
				ret += x-1;
			}else{
				ret--;
			}
		}
		printf("%d\n",ret<0?0:ret);
	}
}