#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<29;

vector<int> eratosthenes(int n) {
  vector<int> v(n);
  for (int i=2; i<n; i++) v[i] = i;
  for (int i=2; i*i<n; i++)
    if (v[i])
      for (int j=i*i; j<n; j+=i)
        v[j] = 0;
  return v;
}

int main() {
  vector<int> prime = eratosthenes(1000000);
  vector<int> psum(2000000);
  int sum = 0;
  REP(i,1000000) {
    if (prime[i]) ++sum;
    psum[i] = sum;
  }
  int n;
  while(cin >> n, n) {
    int res = 0;
    REP(i,n) {
      int p, m;
      cin >> p >> m;
      
      int ue = min(999983, p+m);
      int shita = max(0, p-m-1); 
      int X = psum[ue] - psum[shita];
      // cout << ue << " " << psum[ue] << endl;
      // cout << shita << " " << psum[shita] << endl;
      if (X)
        res += X-1;
      else
        --res;
    }
    cout << res << endl;
  }
}