#include <bits/stdc++.h>

using namespace std;

const int MaxLimit = 1000001;
bool PrimeNumber[MaxLimit] = { false,false, true,true,false };
int Total[MaxLimit] = { 0,0,1,2,2 };

void findPrimeNumber() {
	int limit;
	bool flag;

	for (int i = 5; i < MaxLimit; i++) {
		if (i % 2 == 0) {
			PrimeNumber[i] = false;
			Total[i] = Total[i - 1];
			continue;
		}

		limit = sqrt(i) + 1;
		for (int j = 2; j < limit; j++) {
			if (i%j == 0) {
				flag = false;
				PrimeNumber[i] = false;
				Total[i] = Total[i - 1];
				break;
			}
			flag = true;
		}
		if (flag) {
			PrimeNumber[i] = true;
			Total[i] = Total[i - 1] + 1;
		}
	}
}

int main() {
	int n, p, m, minLimt, maxLimt, cnt, sum;

	findPrimeNumber();
	while (cin >> n, n) {
		sum = 0;
		for (int i = 0; i < n; i++) {
			cnt = 0;
			cin >> p >> m;
			minLimt = max(1, p - m);
			maxLimt = min(MaxLimit-1, p + m);
			cnt = Total[maxLimt] - Total[minLimt-1];
			//cout << p << ":" << m << " = " << cnt << endl;
			if (cnt == 0) {
				--sum;
			}
			else {
				sum += cnt - 1;
			}
		}
		cout << sum << endl;
	}

	return 0;
}

