#include<iostream>
#include<cstdio>

#define MAX 1000000

int eratos[MAX];

void MakePrimes(){
	eratos[0] = 1;
	eratos[1] = 1;
	eratos[2] = 0;
	for(int i = 2; i < MAX; ++i){
		if( eratos[i] == 0 ){
			for(int j = 2; i * j < MAX; ++j){
				eratos[i*j] = 1;
			}
		}
	}
}
int CountPrime(int a, int b){
	int ret = 0;
	a = std::max(0,a);
	b = std::min(MAX-1,b);
	for(int i = a; i <= b; ++i){
		if( eratos[i] == 0 )
			++ret;
	}
	return ret;
}

int main()
{
	MakePrimes();
	while(true){
		int n;
		int ans = 0;
		
		scanf("%d", &n);
		if( n == 0 ) break;

		for(int i = 0 ; i < n ; ++i){
			int X = 0;
			int p,m;
			scanf("%d%d", &p,&m);

			X = CountPrime( p - m, p + m );
			ans += X - 1;
		}

		printf("%d\n", ans);
	}
	return 0;
}