#include <iostream>
#include <algorithm>
using namespace std;
int n, m, p, prime[1000009], s[1000009];
int main() {
	fill(prime + 2, prime + 1000000, 1);
	for (int i = 2; i < 1000; i++) {
		if (!prime[i]) continue;
		for (int j = i * i; j < 1000000; j += i) prime[j] = 0;
	}
	for (int i = 2; i < 1000009; i++) s[i] = s[i - 1] + prime[i];
	while (cin >> n, n) {
		int ret = 0;
		for (int i = 0; i < n; i++) {
			cin >> p >> m;
			ret += s[min(p + m + 1, 1000000)] - s[max(p - m, 0)] - 1;
		}
		cout << ret << endl;
	}
}