#include<iostream>
using namespace std;
int x[2000000];

void prime(int p) {
	x[2] = 1;
	for (int i = 3; i < p; i += 2) {
		for (int j = 3; j < p; j += 2) {
			if (i%j == 0) { goto E; }
		}
		x[i] = 1;
	E:;
	}
}

int n, m, c, sum;

int main() {
	prime(1000000);
	while (true) {
		sum = 0;
		cin >> n;
		if (n == 0) { break; }
		for (int i = 0; i < n; i++) {
			cin >> m >> c;
			for (int j = m - c; j <= m - c; j++) {
				sum += x[j];
			}
			sum -= 1;
		}
		cout << sum << endl;
	}
}