#include <iostream>
#include <cmath>
using namespace std;

int prime[100000],size;
bool flg[1000008];

int isPrime(int x){
	for(int i=0;i<size && prime[i]<=sqrt(x);i++){
		if(x % prime[i] == 0) return false;
	}
	return true;
}

void mkPrime(void){
	prime[0] = 2;
	size = 1;

	flg[0] = flg[1] = false;
	flg[2] = true;

	for(int i=3;i<1000008;i++){
		if(i>999983){
			flg[i] = false;
		}
		else if((flg[i] = isPrime(i)) == true){
			prime[size++] = i;
		}
	}
}

int main(void){
	mkPrime();

	int n;

	while(cin>>n && n){
		int rst = 0;
		while(n--){
			int p,m;
			int count = 0;

			cin>>p>>m;

			int from = p - m;
			int to = p + m;

			if(from<0) from = 0;
			if(from>999983) from = 999984;
			if(to<0) to = 0;
			if(to>999983) to = 999984;

			for(int i=from;i<=to;i++){
				if(flg[i]) count++;
			}

			if(count == 0){
				rst--;
			}
			else{
				count--;
				rst += count;
			}
		}
		cout<<rst<<endl;
	}

	return 0;
}