#include <iostream>
#include <algorithm>

#define mx 1000001

using namespace std;

bool prm[mx];
int cnt[mx];

void mkPrm()
{
  fill(prm, prm+mx, true);
  prm[0] = prm[1] = false;

  for(int i = 2; i < mx; ++i)
    if(prm[i])
      for(int j = i+i; j < mx; j+=i) prm[j] = false;

  int t = 0;
  for(int i = 0; i < mx; ++i){
    if(prm[i]) ++t;
    cnt[i] = t;
  }

  return ;
}


int main()
{
  int n, s, p, m, c;
  mkPrm();

  while(cin>>n && n){
    s = 0;
    while(n--){
      cin >> p >> m;
      c = ((p+m >= mx)? cnt[mx-1]: cnt[p+m]);
      c -= (p-m < 1)? cnt[0] : cnt[p-m-1];
      s--;
      if(c > 0) s += c;
    }
    cout << ((s < 0)? 0 : s) << endl;
  }

  return 0;
}