#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
#include <bitset>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e9 + 7;
const ll MOD = 1e9 + 7;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second

int n;

bool is_prime[2000100];
int cnt[2000100];

int main(){
	fill(is_prime, is_prime + 1000000, 1);
	is_prime[0] = 0;
	is_prime[1] = 0;
	for (int i = 2; i <= (int)1e6; i++){
		if (is_prime[i]){
			for (int j = i * 2; j <= (int)1e6; j += i){
				is_prime[j] = 0;
			}
		}
	}
	FOR(i, 1, 2000000+10){
		if (is_prime[i])cnt[i]++;
		cnt[i] += cnt[i - 1];
	}
	//REP(i, 10){
	//	cout << i << " " << cnt[i] << endl;
	//}
	//for (int i = 999983 - 20; i <= 999983 + 20; i++){
	//	cout << i << " " << cnt[i] << endl;
	//}
	while (cin >> n&&n){
		int ans = 0;
		REP(i, n){
			int p, m;
			cin >> p >> m;
			int x = cnt[p + m];
			if (p - m - 1 >= 0)x -= cnt[p - m - 1];
			ans += (x - 1);
		}
		cout << ans << endl;
	}
}