#include <iostream>
#include <vector>

using namespace std;

int main () {
	vector<int> furui(1000001, 0);
	furui[0] = furui[1] = 1;
	for (int i = 2; i*i < 1000001; i++) {
		if (furui[i] == 0) {
			for (int j = 2; j <= 1000000/i; j++) {
				furui[i*j] = 1;
			}
		}
	}
	int n;
	while (cin >> n) {
		if (n == 0) {
			break;
		}
		int p, m;
		int counter = 0;
		for (int i = 0; i < n; i++) {
			cin >> p >> m;
			int counter2 = 0;
			for (int j = p-m; j <= p+m; j++) {
				if (j < 0) {
					j = 0;
				} else if (j > 1000000) {
					break;
				}
				if (furui[j] == 0) {
					counter2++;
				}
			}
			counter += (counter2-1);
		}
		cout << counter << endl;
	}
	return 0;
}