#include <cstdio>
#include <iostream>
#define MAX 999984
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;
		x = 0;
		for(i=0;i<n;i++){
			cin >> p >> m;
			if(a[p]) x++;
			for(j=1;j<=m;j++){
				if(p - j >= 2 && a[p-j]) x++;
				if(p + j < MAX && a[p+j]) x++;
			}
			x--;
		}
		cout << x << endl;
	}
	return 0;
}