#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define NUM 999986

int* table,*ruisekiwa;
int N;

void func(){

	int prime,m,left,right;
	int to_people = 0,to_king = 0,tmp;

	for(int i = 0; i < N; i++){
		scanf("%d %d",&prime,&m);

		left = ruisekiwa[max(0,prime-m-1)];
		if(prime-m > NUM){
			left = ruisekiwa[NUM-1];
			right = ruisekiwa[NUM-1];
		}else if(prime+m >= NUM){
			right = ruisekiwa[NUM-1];
		}else{
			right = ruisekiwa[prime+m];
		}

		tmp = right - left;
		if(tmp == 0)to_king++;
		else{
			to_people += tmp-1;
		}
	}

	printf("%d\n",to_people-to_king);

}

int main(){

	table = new int[NUM];
	ruisekiwa = new int[NUM];

	for(int i = 0; i < NUM; i++){
		table[i] = 0;
		ruisekiwa[i] = 0;
	}

	int limit = sqrt(NUM);

	for(int i = 2; i <= limit; i++){
		if(table[i] == 0){
			for(int j = 2*i; j < NUM; j+=i){
				table[j] = 1;
			}
		}
	}

	for(int i = 2; i < NUM; i++){
		if(table[i] == 0){
			ruisekiwa[i] = 1;
		}
	}

	for(int i = 3; i < NUM; i++){
		ruisekiwa[i] += ruisekiwa[i-1];
	}

	while(true){
		scanf("%d",&N);
		if(N == 0)break;

		func();
	}

	return 0;
}