#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

const int MAX = 1000000;
bool prime[MAX];
vector<int> V;
int ans;

void make(){
  fill(prime,prime+MAX,1);
  prime[0] = prime[1] = 0;
  for(int i = 2; i*i < MAX; i++)
    if(prime[i])
      for(int j = 2*i; j < MAX; j+=i)
	prime[j] = 0;

  V.push_back(2);
  for(int i = 3; i < MAX; i+=2)
    if(prime[i]) V.push_back(i);

}

int solve(int p, int m){
  int num = upper_bound(V.begin(),V.end(),p+m)-
    lower_bound(V.begin(),V.end(),p-m);
  return num-1;
}
int main(){
  make();
  int num;
  while(cin >> num && num){
    ans = 0;
    for(int i = 0; i < num; i++){
      int p,m;
      cin >> p >> m;
      ans += solve(p,m);
    }
    cout << ans << endl;
  }
  return 0;
}