#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstdlib>
using namespace std;
#define REP(i, j) for(int i = 0; i < j; ++i)
#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define P pair<int, int>
const int N = 1000000;
const int RATE = 9973;

int pr[N];

void prime(){
  REP(i, N) pr[i] = 1;
  pr[0] = 0; pr[1] = 0;
  for(int i = 2; i < N; ++i){
    if(!pr[i]) continue;
    for(int j = i + i; j < N; j += i) pr[j] = 0;
  }
  FOR(i, 1, N) pr[i] += pr[i - 1];
}

int main() {
  prime();
  int n;
  while(cin >>n && n){
    //map<int, int> m;
    int ans = 0;
    REP(i, n){
      int p, m; cin >>p >>m;
      //m[p] += (pr[min(N - 1, p + m)] - pr[max(0, p - m - 1)]);
      int tmp = (pr[min(N - 1, p + m)] - pr[max(0, p - m - 1)]);
      if(tmp == 0) --ans;
      else ans += (tmp - 1);
    }
    cout <<ans <<endl;
    //int ans = 0;
    //for(map<int, int>::iterator it = m.begin(); it != m.end(); ++it){
    //  if((*it).second == 0) --ans;
    //  else ans += ((*it).second - 1);
    //}
    //cout <<max(0, ans) <<endl;
  }
  return 0;
}