
#include<iostream>
using namespace std;
#define MAX 1000000
int main()
{
  int b[MAX+1]={0},i,j;
  b[0]=b[1]=1;
  for(i=4;i<=MAX;i+=2)b[i]=1;
  for(i=3;i*i<=MAX;i+=2)
    if(!b[i])for(j=i+i;j<=MAX;j+=i)b[j]=1;
  int c[MAX+1]={0};
  for(i=1;i<=MAX;i++)c[i]=c[i-1]+!b[i];

  int n,p,m,X,f,l;
  while(cin>>n,n){
    X=0;
    for(i=0;i<n;i++){
      cin>>p>>m;
      f=p-m-1;if(f<0)f=0;
      l=p+m;if(l>MAX)l=MAX;
      X+=c[l]-c[f]-1;
    }
    cout<<X<<endl;
  }
}