#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
	
	bool a[1000001];
	int pr[78499], p, n, m, t, q;
	
	fill(a, a+1000001, true);
	a[0] = a[1] = false;
	
	for(int i = 2; i <= 1000000; i++){
		if(a[i]){
			for(int j = 2*i; j <= 1000000; j+=i){
				a[j] = false;
			}
		}
	}
	
	while(1){
		cin >> n;
		if(n == 0){
			break;
		}
		t = 0;
		while(n--){
			cin >> p >> m;
			for(int i = (p-m < 0)? 2: p-m, max = (m+p > 1000000)? 999983: m+p; i <= max; i++){
				if(a[i]){
					bool f = true;
					for(int j = 0; j < q; j++){
						if(pr[j] == i){
							f = false;
							break;
						}
					}
					if(f){
						pr[q++] = i;
						t++;
					}
				}
			}
			t--;
			if(t < 0){
				t = 0;
			}
			cout << t << endl;
		}
	}
	
	return 0;
}