#include <iostream>
#include <vector>
using namespace std;
vector<bool> prime(1000001,true);

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

int main(){
	p_init();
	int x,n,p,m;
	int ret;
	while(cin >> n && n){
		ret = 0;
		for(int i=0;i<n;i++){
			cin >> p >> m;
			int mi = p<m ? 0 : p-m;
			int mx = p+m>1000000?1000000:p+m;
			x = 0;
			for(int o=mi;o<=mx;o++){
				if(prime[o]){
					x++;
				}
			}
			if(x){
				ret += x-1;
			}else{
				ret--;
			}
		}
		cout << (ret<0?0:ret) << endl;
	}
}