#include <iostream>
using namespace std;
int a[1000000][2];
int main()
{
  int i,j,k,n,x,s;
  k = 0;
  for(i=2;i<1000000;i++){
    if(!a[i][0]){
      k++;
      for(j=2;i*j<1000000;j++){
	a[i*j][0] = 1;
      }
    }
    a[i][1] = k;
  }
  for(;;){
    cin >> n;
    if(n==0)break;
    s = 0;
    while(n--){
      cin >> i >> j;
      k = (i+j) >= 1000000 ? 999999 : i+j;
      x = a[k][1];
      k = (i-j-1) < 0 ? 0 : i-j-1;
      x -= a[k][1];
      if(x > 0) s += x-1;
      else s--;
    }
    cout << s << endl;
  }
  return 0;
}