#include <iostream>
#include <string.h>
using namespace std;
#define LIM 1000000

bool is_prime[LIM+10];
int sum[LIM + 10];

int main(){
	memset(is_prime, true, sizeof(is_prime));
	is_prime[0] = is_prime[1] = false;

	for(int p=2;p*p<=LIM;p++)if(is_prime[p]){
		for(int k=p*p;k<=LIM;k+=p)is_prime[k] = false;
	}

	sum[0] = 0;
	for(int i=0;i<=LIM;i++){
		sum[i+1] = sum[i] + is_prime[i];
	}


	int n;
	while(cin >> n && n){
		int pay = 0;
		for(int i=0;i<n;i++){
			int p, m;
			cin >> p >> m;

			int low = max(0, p - m);
			int high = min(LIM, p + m);

			pay += sum[high + 1] - sum[low] - 1;
		}

		cout << pay << endl;
	}
	return 0;
}