#include <iostream>
#include <memory>
#include <memory.h>
#include <cmath>
#include <fstream>
#include <cmath>
#include <numeric>
#include <vector>
#include <stack>
#include <string>
#include <queue>
#include <sstream>
#include <cstdlib>
#include <cassert>
#include <cstdio>
#include <map>
#include <iomanip>
#include <list>
#include <cctype>
#include <algorithm>
#include <complex>
#define USE_MATH_DEFINES
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, m, n) for(int i = m; i < n; i++)
using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const int INF = 1 << 29;
const double EPS = 1E-10;

bool prime[2000000];
int cnt[2000000];

int main(){
	fill(prime, prime + 2000000, true);
	prime[0] = prime[1] = false;
	for(int i = 2; i * i < 2000000; i++){
		if(prime[i]){
			for(int j = 2 * i; j < 2000000; j += i) prime[j] = false;
		}
	}
	int c = 0;
	rep(i, 2000000){
		if(prime[i])c++;
		cnt[i] = c;
	}

	int n, x, p, m, a, b;
	while(cin >> n && n){
		x = 0;
		rep(i, n){
			cin >> p >> m;
			x--;
			a = max(0, p - m - 1);
			b = min(999983, p + m);
			x += cnt[b] - cnt[a];
		}
		cout << x << endl;
	}
	return 0;
}