#include <stdio.h>
#include <iostream>
#include <vector>
#include <list>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <iterator>
#include <cstdlib>

using namespace std;

#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) (EQ((a).real(),(b).real()) && EQ((a).imag(),(b).imag()))

typedef complex<double> P;
typedef long long ll;

const int MAX_SIZE = 10000;
const int MAX_PRIME = 1000000;


int prime[MAX_PRIME+1];
bool isPrime[MAX_PRIME+1];
int p;

void erats(){
	// primeツδ環スツトツづーツ渉可甘コツ可サ
	fill(isPrime,isPrime+MAX_PRIME,true);
	p = 0;
	isPrime[0] = isPrime[1] = false;

	for(int i = 2; i <= MAX_PRIME; i++){
		if(isPrime[i]){
			prime[p++] = i;
		}
		else
			continue;

		// ツづ督づゥツつ「ツづ可つゥツつッツづゥ
		for(int j = 2*i; j <= MAX_PRIME; j+=i){
			isPrime[j] = false;
		}
		cout << flush;
	}
}


int main(){

	// ツ素ツ青板づーツ仰づ淞づゥ
	erats();

	int n;
	while(cin>>n&&n!=0){
		int sum=0;
		for(int i = 0; i < n; i++){
			int pp,mm;
			cin>>pp>>mm;
			int cnt=0;

			int *lb = lower_bound(prime,prime+p,pp-mm);
			for(int *it = lb;; it++){
				if(*it<=pp+mm){
					cnt++;
				}
				else if(*it>pp+mm||*it>=999983)
					break;
			}

			//for(int j = 0; j < p; j++){
			//	if(prime[j]>=pp-mm&&prime[j]<=pp+mm){
			//		cnt++;
			//	}
			//	else if(prime[j]>pp+mm)
			//		break;
			//}
			sum+=max(0,cnt-1);
			if(cnt==0)
				sum--;
		}
		cout<<max(0,sum)<<endl;
	}

	return 0;
}