#include<cstdio>
#include<bitset>
#include<algorithm>
using namespace std;
int main()
{
	const int N = 1000000;
	bitset<N> isPrime;
	static int lpCount[N] = {0};
	int i,j;
	isPrime.set();
	isPrime[0] = isPrime[1] = false;
	for(i = 2; i < N; ++i)
	{
		if(isPrime[i])
		{
			for(j = i*2; j<N; j+=i)
				isPrime[j] = false;
			lpCount[i] = lpCount[i-1]+1;
		}
		else
			lpCount[i] = lpCount[i-1];
	}

	int p,m,n,s,l,r;
	while(scanf("%d",&n),n)
	{
		s=0;
		while(n--)
		{
			scanf("%d%d",&p,&m);
			l = max(0,p-m);
			r = min(N-1,p+m);
			s += (lpCount[r]-lpCount[l]+isPrime[l])-1;
		}
		printf("%d\n",s);
	}
	
	return 0;
}