#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
 
using namespace std;
 
#define pb(n)	push_back(n)
#define mp(n,m) make_pair(n,m)
#define fi 	first
#define se 	second
#define all(r) (r).begin(),(r).end()
 
#define REP(i,s,e)	for(int i=(s); i<(e); i++)
#define rep(i,n)	REP(i,0,n)
#define REPE(i,s,e)	for(int i=s; i>e; i--)
#define repe(i,n)	REPE(i,n,-1)

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
 
typedef vector<vi> vii;
 
const int IMAX=((1<<30)-1)*2+1;
const int INF=100000000;
const double EPS=1e-10;
//int mod=1000000007
 

 

int MP=999983;

vi is_prime(MP+1,1);
vi sum_prime(MP+1,0);


void set_prime(){
	is_prime[0]=is_prime[1]=0;
	for(int i=2; i<=MP; i++){
		if(is_prime[i]){
			for(int j=i*2; j<MP; j+=i){
				is_prime[j]=0;
			}
		}
		sum_prime[i]=sum_prime[i-1]+is_prime[i];
	}
}

int main(){
	set_prime();
	int n,p,m;
	int x;
	while(cin>>n && n>0){
		int ans=0;

		rep(i,n){
			cin>>p>>m;
			x=(p+m>MP?sum_prime[MP]:sum_prime[p+m])-(p-m-1>0?sum_prime[p-m-1]:0);
			if(x==0) ans--;
			else{
				ans+=x-1;
			}
		}
		cout<<ans<<endl;
	}
}