#include <iostream>
#include <algorithm>
using namespace std;

const int mx = 1e6+3;
bool tab[mx+100];
int  amt[mx+100];

int main() {
	for(int i = 0; i < mx+100; i++)
		tab[i] = true;
	tab[0] = tab[1] = false;
	for(int i = 2; i*i < mx+100; i++) {
		if(tab[i]) {
			for(int j = i*2; j < mx+100; j += i) {
				tab[j] = false;
			}
		}
	}

	for(int i = mx; i < mx+100; i++)
		tab[i] = false;

	for(int i = 1; i < mx+100; i++) {
		amt[i] = amt[i-1];
		if(tab[i]) amt[i]++;
	}

	int n;
	while(cin >> n, n) {
		int res = 0;
		for(int i = 0; i < n; i++) {
			int p, m;
			cin >> p >> m;
			res += (amt[min(mx-1,p+m)] - amt[max(0,p-m-1)]) - 1;
		}
		cout << res << endl;
	}
}