#include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<sstream>
#include<iomanip>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<double> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int,pii> pip;
typedef vector<pip>vip	;
const double PI=acos(-1);
const double EPS=1e-8;
const int inf=1<<30;
int sosu[1000001]={1,1,0};
int main(){
	for(int i=2;i*i<=1000001;i++)if(sosu[i]==0)
	for(int j=i*2;j<1000001;j+=i)sosu[j]=true;
	rep(i,1000001)sosu[i]^=1;
	rep(i,1000000)sosu[i+1]+=sosu[i];
	
	int n;
	while(cin>>n,n){
		int out=0;
		rep(i,n){
			int a,b;
			cin>>a>>b;
			int s=max(0,a-b-1),t=min(a+b,1000000);
			int co=sosu[t]-sosu[s];
			if(co==0)out--;
			else out+=co-1;
		}
		cout<<out<<endl;
	}
}