using namespace std;
int N=1000000,sqrtN=1000;
bool isprime[1000001];

void sieve_of_atkin() {
  int n;
  for (int z = 1; z <= 5; z += 4) {
    for (int y = z; y <= sqrtN; y += 6) {
      for (int x = 1; x <= sqrtN && (n = 4*x*x+y*y) <= N; ++x)
        isprime[n] = !isprime[n];
      for (int x = y+1; x <= sqrtN && (n = 3*x*x-y*y) <= N; x += 2)
        isprime[n] = !isprime[n];
    }
  }
  for (int z = 2; z <= 4; z += 2) {
    for (int y = z; y <= sqrtN; y += 6) {
      for (int x = 1; x <= sqrtN && (n = 3*x*x+y*y) <= N; x += 2)
        isprime[n] = !isprime[n];
      for (int x = y+1; x <= sqrtN && (n = 3*x*x-y*y) <= N; x += 2)
        isprime[n] = !isprime[n];
    }
  }
  for (int y = 3; y <= sqrtN; y += 6) {
    for (int z = 1; z <= 2; ++z) {
      for (int x = z; x <= sqrtN && (n = 4*x*x+y*y) <= N; x += 3)
        isprime[n] = !isprime[n];
    }
  }
  for (int n = 5; n <= sqrtN; ++n)
    if (isprime[n])
      for (int k = n*n; k <= N; k+=n*n)
        isprime[k] = false;
  isprime[2] = isprime[3] = true;
}

int main(){
	sieve_of_atkin();
	int n,p,m,c,S,mn,mx;
	while(scanf("%d",&n),n){
		S=0;
		for(int j=0;j<n;j++){
			scanf("%d%d",&p,&m); mn=p-m; if(mn<2)mn=2; mx=p+m; if(mx>N-1)mx=N-1;
			c=0; for(int i=mn;i<=mx;i++)if(isprime[i])c++;
			if(c>0)c--; else c=-1;
			S+=c;
		}
		printf("%d\n",S);
	}
	return 0;
}