#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <queue>
#define MAX 1000001
using namespace std;

int main(void){
	int i,j,k,n,m,p,x,sum;
	bool a[MAX];

	for(i=0;i<MAX;i++) a[i] = true;
	for(i=2;i<MAX;i++){
		if(a[i]){
			for(j=i+i;j<MAX;j+=i) a[j] = false;
		}
	}

	while(1){
		cin >> n;
		if(n == 0) break;
		sum = 0;
		for(i=0;i<n;i++){
			x = 0;
			cin >> p >> m;
			for(j=p-m;j<=p+m;j++){
				if(j < 2) continue;
				if(j >= MAX) break;
				if(a[j]) x++;
			}
			//if(x == 0) x = 1;
			sum += x - 1;
		}
		cout << sum << endl;
	}
	return 0;
}