#include <cstdio>
#include <algorithm>

using namespace std;

bool p[1000001];
int cnt[1000001];
int main()
{
	p[2] = true;
	
	for (int i = 3; i <= 1000000; i += 2) p[i] = true;
	
	for (int i = 3; i <= 1000000; i += 2){
		if (p[i]){
			for (int j = 3 * i; j <= 1000000; j += 2 * i)
				p[j] = false;
		}
	}
	
	for (int i = 1; i <= 1000000; i++){
		cnt[i] = cnt[i - 1] + p[i];
	}

	int n;
	
	while (scanf("%d", &n) && n){
		int ans, a, b;
		ans = 0;
		for (int i = 0; i < n; i++){
			scanf("%d %d", &a, &b);
			ans += cnt[min(1000000, a + b)] - cnt[max(0, a - b - 1)] - 1;
		}
		printf("%d\n", ans);
	}
	
	return (0);
}