#include <bits/stdc++.h>
using namespace std;

int prime[1000001];

int main()
{
	for (int i = 2; i <= 1000000; i++){
		prime[i] = 1;
	}
	for (int i = 2; i * i <= 1000000; i++){
		for (int j = i * i; j <= 1000000; j += i){
			prime[j] = 0;
		}
	}
	for (int i = 1; i <= 1000000; i++){
		prime[i] += prime[i - 1];
	}
	
	int n;
	while (scanf("%d", &n), n){
		int res = 0;
		for (int i = 0; i < n; i++){
			int p, m;
			scanf("%d %d", &p, &m);
			int x = prime[min(p + m, 1000000)] - prime[max(p - m - 1, 0)];
			res += x ? x - 1 : -1;
		}
		printf("%d\n", res);
	}
	
	return 0;
}