#include <iostream>
#include <vector>
using namespace std;

const int MAX = 1000000;
const int MAX_P = 999983;
int prseq[MAX] = {};
int count[MAX + 1] = {};

void prime(){
	prseq[0] = 1;
	
	for(int i = 2; i <= MAX; ++i){
		int k = i * 2;
		while(k <= MAX_P){
			prseq[k - 1] = 1;
			k += i;
		}
	}
	for(int i = MAX_P + 1; i <= MAX; ++i){
		prseq[i - 1] = 1;
	}
	
	for(int i = 1; i <= MAX; ++i){
		count[i] = count[i - 1] + (prseq[i - 1] ? 0 : 1);
	}
}

int main(){
	int n, p, m;
	
	prime();
	
	while(cin >> n, n){
		int sum = 0;
		for(int i = 0; i < n; ++i){
			cin >> p >> m;
			int x = count[p + m > MAX ? MAX : p + m] - count[p - m < 1 ? 0 : p - m - 1];
			sum += x - 1;
		}
		cout << sum << endl;
	}
}