#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<bool> used;
long long sum[1000001];

int main() {
	used.resize(1000001, false);
	used[0] = used[1] = true;
	for (int i = 2; i * i <= 1000001; i++) {
		if (!used[i]) {
			sum[i]++;;
			for (int j = i; j <= 1000001; j += i) used[j] = true;
		}
	}
	REP(i, 1000001) if (!used[i]) sum[i]++;
	
	FOR(i, 1, 1000001) sum[i] += sum[i - 1];
	
	int n;
	while (cin >> n, n) {
		long long ans = 0;
		REP(i, n) {
			int p, m;
			scanf("%d %d", &p, &m);
			
			int a = p - m < 0 ? 0 : p - m - 1;
			int b = p + m > 1000001 ? 1000000 : p + m;
			
			int cnt = sum[b] - sum[a];
			if (cnt == 0) ans--;
			else ans += cnt - 1;
		}
		cout << ans << endl;
	}
	
	return 0;
}