#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

#define NMAX 1000000

int main() {
  int n, p, m, x, v;
  int a[NMAX+1];
  a[0] = a[1] = 0;
  for (int i=2; i<=NMAX; i++) {
    a[i] = 1;
  }
  for (int i=2; i*i<=NMAX; i++) {
    if (!a[i]) continue;
    for (int j=i+i; j<=NMAX; j+=i) {
      a[j] = 0;
    }
  }
  for (int i=1; i<=NMAX; i++) {
    a[i] += a[i-1];
  }

  while (1) {
    cin >> n;
    if (!n) break;
    v = 0;
    for (int i=0; i<n; i++) {
      cin >> p >> m;
      x = a[min(p+m,NMAX)] - a[max(0,p-m-1)];
      v += x-1;
    }
    cout << max(v,0) << endl;
  }
  return 0;
}