#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>

using namespace std;

#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 fi first
#define se second

#define PI acos(-1.0)
#define INF 10e15+9
#define MOD 1000000007
#define EPS 1e-10
#define MAX_N 100100
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

#define MP 999983

int n;
int p, m;

bool is_prime[MP + 100];

int main(){
	fill(is_prime, is_prime + MP + 100, 1);
	is_prime[0] = 0;
	is_prime[1] = 0;
	for (int i = 2; i <= MP; i++){
		if (is_prime[i]){
			for (int j = i * 2; j <= MP; j += i){
				
				is_prime[j] = 0;
			}
		}
	}
	while (cin >> n&&n){
		int ans = 0;
		REP(i, n){
			cin >> p >> m;
			for (int i = max(p - m, 2); i <= min(p + m, MP); i++){
				if (is_prime[i]){
					ans++;
				}
			}
		}
		cout << ans - n << endl;
	}
}