#include <iostream>
#include <vector>

using namespace std;

#define MP 999983

int main() {
	vector<int> data(MP+1, 0);
	data[0] = data[1] = 1;
	for (int i = 2; i*i <= MP; i++) {
		if (data[i] == 0) {
			for (int j = 2; i*j <= MP; j++) {
				data[i*j] = 1;
			}
		}
	}
	int n;
	while (cin >> n) {
		if (n == 0) {
			break;
		}
		int p, m;
		int counter2 = 0;
		//vector<int> data2(MP+1, 0);
		for (int i = 0; i < n; i++) {
			cin >> p >> m;
			int ma, mi;
			ma = min(p+m, MP);
			mi = max(p-m, 0);
			int counter = 0;
			for (int j = mi; j <= ma; j++) {
				if (data[j] == 0) {
					counter++;
				}
			}
			counter2 += counter-1;
			//data2[m] += counter;
		}
		/*for (int i = 0; i <= MP; i++) {
			if (data2[i] > 0) {
				cout << data2[i] << endl;
				counter2 += data2[i]-1;
			}
		}*/
		cout << counter2 << endl;
	}
	return 0;
}