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

#define P_MAX 999984
using namespace std;

vector<int> Prime;
bool isPrime[P_MAX];

void make() {
  fill(isPrime, isPrime+P_MAX, true);
  isPrime[0] = isPrime[1] = false;
  for(int i = 2; i < P_MAX; ++i) {
    if(isPrime[i]) {
      Prime.push_back(i);
      for(int j = i*2; j < P_MAX; j += i) isPrime[j] = false;
    }
  }
}

int n, p, m, sum;

int calcX() {
  int a = p - m;
  int b = p + m;
  vector<int>::iterator it1 = lower_bound(Prime.begin(), Prime.end(), a);
  vector<int>::iterator it2 = upper_bound(Prime.begin(), Prime.end(), b);
  return distance(it1, it2);
}

void add() {
  int X = calcX();
  if(X == 0) sum -= 1;
  else       sum += X - 1;
}

main() {
  make();
  while((cin >> n) && n) {
    sum = 0;
    for(int i = 0; i < n; ++i) {
      cin >> p >> m;
      add();
    }
    cout << sum << endl;
  }
}