#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <deque>
#include <cstdio>
#include <cmath>
#include <complex>
#include <iostream>
#include <iterator>

using namespace std;

#define reep(i,f,t) for(int i=f ; i<int(t) ; ++i)
#define rep(i,n) reep(i, 0, n) 

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

const int MAX = 1000000;

int main()
{
	vi prime(MAX+1, 1);
	prime[0] = prime[1] = 0;
	for(int i=2; i*i<=MAX; ++i){
		if(prime[i]){
			for(int j=i*2; j<=MAX; j+=i)
				prime[j] = 0;
		}
	}
	reep(i, 1, MAX+1)
		prime[i] += prime[i-1];
	
	int n;
	while(scanf("%d", &n), n){
		int sum = 0;
		rep(i, n){
			int p, m;
			scanf("%d%d", &p, &m);
			sum += prime[min(p+m, MAX)] - prime[max(p-m-1, 0)];
		}
		
		printf("%d\n", sum - n);
	}
	
	return 0;
}