#include<bits/stdc++.h>
#define MOD 1000000007
#define EPS (1e-10)
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;

bool is_prime[999984];
int prime[100000];
int main() {
	memset(is_prime, 1, sizeof(is_prime));
	int c = 0;
	is_prime[0] = is_prime[1] = false;
	for (int i = 2; i <= 999983; i++) {
		if (is_prime[i]) {
			prime[c++] = i;
			for (int j = i * 2; j <= 999983; j += i)is_prime[j] = false;
		}
	}
	int n;
	while (scanf("%d", &n), n) {
		int sum = 0;
		rep(i, n) {
			int p, m; scanf("%d%d", &p, &m);
			int a = upper_bound(prime, prime + c, p + m) - prime - 1;
			int b = lower_bound(prime, prime + c, p - m) - prime - 1;
			sum += a - b - 1;
		}
		printf("%d\n", sum);
	}
}