#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

int main(){
  const int MAXP = 999983;
  vector<bool> is_prime(MAXP + 1, true);
  is_prime[0] = is_prime[1] = false;
  for(int i = 2; i * i <= MAXP; i++)if(is_prime[i]){
    for(int j = i * i; j <= MAXP; j += i){
      is_prime[j] = false;
    }
  }
  vector<int> acc(MAXP + 1, 0);
  for(int i = 1; i <= MAXP; i++){
    acc[i] = acc[i - 1] + (is_prime[i] ? 1 : 0);
  }
  int N;
  while(cin >> N && N != 0){
    int ans = 0;
    REP(i, N){
      int p, m; cin >> p >> m;
      int left = max(p - m, 1);
      int right = min(p + m, MAXP);
      int cnt = acc[right] - acc[left - 1];
      ans += cnt - 1;
    }
    if(ans < 0) ans = 0;
    cout << ans << endl;
  }
  return 0;
}