#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

#define M 1000000

int m[3] = { 2, 7, 61 }; 

long mod(long a, long d, long n){
	if(a==0 || n==0) return 0;
	if(d==0) return 1 % n;
	
	unsigned long currentMod = a % n;
	unsigned long currentValue = (d%2 == 1) ? currentMod : 1;
	
	for(d>>=1; d>0; d>>=1){
		currentMod = (currentMod * currentMod) % n;
		if(d%2 == 1){
			currentValue = (currentValue * currentMod) % n;
		}
	}
	
	return currentValue;
}

bool isPrime(long x){
	if(x==2){
		return true;
	}
	else if(x%2 == 0){
		return false;
	}
	else if(x != 3 && x%6 != 1 && x%6 != x-1){
		return false;
	}
	else{
		long s = 0;
		long d = x - 1;
		while(d%2 == 0){
			d = d / 2;
			s++;
		}
		
		for(int i=0; i<3; i++){
			long a = m[i];
			if(a%x == 0) continue;
			
			long pow = mod(a, d, x);
			if(pow == 1) continue;
			if(pow == x - 1) continue;
			
			bool con2 = false;
			for(int r=1; r<s; r++){
				pow = mod(pow, 2, x);
				if(pow ==  x - 1){
					con2 = true;
					break;
				}
			}
			
			if(!con2){
				return false;
			}
		}
		
		return true;
	}
}

int main(int argc, char** argv)
{
	char str[128];
	int* prime;
	int i, c, n, pay, size, cen, ran;

	prime = new int[M];

	while(true){
		cin >> str;
		size = atoi(str);
		if(size == 0) break;

		pay = 0;
		for(n=0; n<size; n++){
			cin >> str;
			cen = atoi(str);
			cin >> str;
			ran = atoi(str);
			c = 0;
			for(i=max(2, cen-ran); i<=min(cen+ran, M); i++){
				if(prime[i] != 0 && prime[i] != 1){
					if(isPrime(i)){
						prime[i] = 1;
						c++;
					}
					else{
						prime[i] = 0;
					}
				}
				else{
					if(prime[i]==1){
						c++;
					}
				}
			}

			pay += c - 1;
		}

		cout << max(0, pay) << endl;
	}

	delete prime;
}