#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

#define MP 999983

int main() {
	vector<int> data(MP+1, 0);
	data[0] = data[1] = 1;
	vector<int> data2;
	for (int i = 2; i*i <= MP; i++) {
		if (data[i] == 0) {
			for (int j = 2; i*j <= MP; j++) {
				data[i*j] = 1;
			}
		}
	}
	for (int i = 0; i <= MP; i++) {
		if (data[i] == 0) {
			data2.push_back(i);
		}
	}
	int n;
	while (cin >> n) {
		if (n == 0) {
			break;
		}
		int p, m;
		int counter2 = 0;
		for (int i = 0; i < n; i++) {
			cin >> p >> m;
			int ma, mi;
			ma = min(p+m, MP);
			mi = max(p-m, 0);
			vector<int>::iterator ite = lower_bound(data2.begin(), data2.end(), mi);
			vector<int>::iterator ite2 = upper_bound(data2.begin(), data2.end(), ma);
			//cout << ite2-ite << endl;
			counter2 += ite2-ite-1;
		}
		cout << counter2 << endl;
	}
	return 0;
}