#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

#define pb push_back
#define sz size()
#define clr clear()
#define M 999983

vector<bool> pt;
vector<int> pa;

void MakeTable(int n){
  pt.clr;
  pt.pb(false);pt.pb(false);pt.pb(true);
  
  while((int)pt.sz<=n+1){
    pt.pb(true);
    pt.pb(false);
  }
  
  int i = 3;
  while(i*i<=n){
    for(int j=3*i;j<=n;j+=2*i)pt[j]=false;
    do{i+=2;}while(!pt[i]);
  }
}

int main(){
  int n,p,m,pn[1000000];
  long long ans;

  MakeTable(M);
  int cnt = 0;
  for(int i=0;i<=M;i++){
    if(pt[i])cnt++;
    pn[i] = cnt;
  }

  for(;;){
    cin >> n;
    if(!n)break;

    ans = 0;
    for(int i=0;i<n;i++){
      cin >> p >> m;
      int l = max(0,p-m-1),r = min(M,p+m);

      if(pn[r]-pn[l]>0)ans += pn[r] - pn[l] - 1;
      else ans--;
    }
    cout << ans << endl;
  }
}