#define _USE_MATH_DEFINES
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <iterator>
 
using namespace std;
 
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
 
#define FOR(i,n) for(int i = 0; i < (n); i++)
#define sz(c) ((int)c.size())

typedef unsigned long long ull;
const int INF = (int)1e8;

bool prime[1000001]; //10^6
int t[1000001*2];

//素数列挙
void init_prime()
{
	memset(prime,1,sizeof(prime));
	prime[0] = prime[1] = false;
	for(int i = 2; i < sizeof(prime); i++) if(prime[i])
		for(int j = i * 2; j < sizeof(prime); j += i) prime[j] = false;
	t[0] = 0;
	for(int i = 0; i < sizeof(prime); i++) t[i+1] = t[i] + prime[i+1];
	for(int i = sizeof(prime); i < sizeof(t)/sizeof(int); i++) t[i+1] = t[i];
}

int main(){
	init_prime();
	int n;
	while(cin>>n,n){
		int p = 0;
		FOR(i,n){
			int x,y; cin>>x>>y;
			int cnt = t[x+y]-t[max(x-y-1,0)];
			p += cnt-1;
		}
		cout << p << endl;
	}

	return 0;
}