#define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;

static const double eps = 10e-8;


bool primeTable[1000001];

int main(){
	fill((bool*)primeTable,(bool*)primeTable+1000001,true);

	primeTable[0] = false;
	primeTable[1] = false;

	for(int i=1;i*i<=1000000;i++){
		if(primeTable[i]){
			for(int j=i+i;j<=1000000;j+=i){
				primeTable[j] = false;
			}
		}
	}


	int n;
	while(~scanf("%d",&n)){
		if(n==0) break;

		int sum=0;
		for(int i=0;i<n;i++){
			int p,m;
			scanf("%d %d",&p,&m);

			int tmp=0;
			for(int j= p-m >= 0 ? p-m : 0;j<=p+m && j<=1000000;j++){
				if(primeTable[j]){
					tmp++;
				}
			}

			tmp > 0 ? sum+=tmp-1 : sum--;
		}

		printf("%d\n",sum >= 0 ? sum : 0);
	}
}