#include <iostream>
using namespace std;

int pri[1000000+1];
bool is_prime[1000000+1];
int prime(int n){
	int p=0;
	for(int i=0;i<n+1;i++){
		is_prime[i] = true;
	}
	is_prime[0] = is_prime[1] = false;
	for(int i=2;i<n+1;i++){
		if(is_prime[i]==true){
			pri[p++] = i;
			for(int j=i+i;j<n+1;j+=i){
				is_prime[j] = false;
			}
		}
	}
	return p;
}

int main()
{
	int max_p = prime(1000000);
	int n;
	while(cin >> n){
		if(!n)
			break;
		int p,m;
		int money = 0;
		for(int i=0;i<n;i++){
			cin >> p >> m;
			int sum = 0;
			for(int j=0;j<max_p;j++){
				if(p-m <= pri[j] && pri[j] <= p+m)
					sum++;
			}
			money += sum;
		}
		money -= n;
		cout << money << endl;
	}	
	return 0;
}