#include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define INF (1<<29)

vector<int> prime;
vector<bool> used;

int main() {
	used.resize(1000001, false);
	used[0] = used[1] = true;
	prime.push_back(-INF);
	for (int i = 2; i * i <= 1000001; i++) {
		if (!used[i]) {
			prime.push_back(i);
			for (int j = i; j <= 1000001; j += i) used[j] = true;
		}
	}
	REP(i, 1000001) if (!used[i]) prime.push_back(i);
	
	int n;
	while (cin >> n, n) {
		long long ans = 0;
		REP(i, n) {
			int p, m;
			scanf("%d %d", &p, &m);
			vector<int>::iterator it1 = upper_bound(prime.begin(), prime.end(), p + m) - 1;
			vector<int>::iterator it2 = upper_bound(prime.begin(), prime.end(), p - m) - 1;
			int cnt;
			
			if (*it1 < p - m) {
				if (*(it1 + 1) > p + m) it1 = prime.end();
				else it1++;
			}
			if (*it2 < p - m) {
				if (*(it2 + 1) > p + m) it2 = prime.end();
				else it2++;
			}
			
			if (it1 == prime.end() || it2 == prime.end())
				cnt = 2 - (it1 == prime.end()) - (it2 == prime.end()); 
			else
				cnt = it1 - it2 + 1;
			
			if (cnt == 0) ans--;
			else ans += cnt - 1;
		}
		cout << ans << endl;
	}
	
	return 0;
}