#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

int count[1000001];
int pos[1000001];
int p[80000],size;

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

void mkPrime(void){
	memset(pos,-1,sizeof(pos));
	p[0] = 2;
	pos[0] = pos[1] = pos[2] = 0;
	size = 1;

	for(int i=3;i<=999983;i++){
		if(isPrime(i)){
			p[size] = i;
			for(int j=i;pos[j]==-1;j--) pos[j] = size;
			size++;
		}
	}

	for(int i=999984;i<=1000000;i++){
		pos[i] = size;
	}
}

int calX(int from,int to){
	int res = 0;
	for(int i=pos[from];i < size && p[i]<=to;i++,res++);
	return res;
}

int main(void){
	int n;

	mkPrime();

	while(scanf("%d",&n) && n){
		memset(count,-1,sizeof(count));
		while(n--){
			int p,m;
			scanf("%d%d",&p,&m);
			if(count[p] == -1) count[p] = 0;
			count[p] += calX(p-m<0?0:p-m, p+m>1000000?1000000:p+m);
		}

		int ans = 0;
		for(int i=0;i<=1000000;i++){
			if(count[i] > 0) ans += count[i] - 1;
			else if(count[i] == 0) ans--;
		}

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

	return 0;
}