#include<iostream>
using namespace std;
bool a[1000001];
int main() {
	fill(a, a + 1000001, true);
	a[0] = false; a[1] = false;
	for (int i = 2; i <= 999983; i++) {
		if (a[i]) {
			int j = 2;
			while (i*j <= 1000000) {
				a[i*j] = false;
				j++;
			}
		}
	}
	int n;
	while (cin >> n&&n != 0) {
		long long sum = 0;
		for (int i = 0; i < n; i++) {
			int p, m, X = 0, k;
			cin >> p >> m;
			k = (p - m);
			if ((p - m) < 0) k = 0;
			for (int j = k; (j <= p + m && j <= 1000000); ) {
				if (a[j]) X++;
				if (j % 2 == 0 && j != 2) j+=2;
				else j++;
			}
			if (X == 0) sum -= 1;
			else sum += (X - 1);
		}
		if (sum < 0) cout << "0" << endl;
		else cout << sum << endl;
	}
}