#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iostream>
#include <sstream>
#include <climits>
#include <cfloat>
#include <complex>

using namespace std;

bool isPrime[1000000];
vector<int> primes;

int main()
{
	fill(isPrime, isPrime+1000000, true);
	for (int i = 2; i < 1000000; ++i)
	{
		if (isPrime[i])
		{
			for (int j = 2*i; j < 1000000; j += i)
				isPrime[j] = false;
			primes.push_back(i);
		}
	}

	int n;
	while (scanf("%d", &n), n)
	{
		int ans = 0;
		while (n--)
		{
			int p, m;
			scanf("%d %d", &p, &m);
			int X = upper_bound(primes.begin(), primes.end(), p+m)
				- lower_bound(primes.begin(), primes.end(), p-m);
			if (X)
				ans += X - 1;
			else
				--ans;
		}
		printf("%d\n", ans);
	}

	return 0;
}