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

using namespace std;

int main()
{
  int n, m, p, s, t;
  bool a[1000000];
  fill(a, a+1000000, true);
  a[0] = a[1] = false;
  vector<int> vec;

  for(int i = 2; i < 1000000; i++){
    if(a[i]){
      vec.push_back(i);
      for(int j = i+i; j < 1000000; j += i){
	a[j] = false;
      }
    }
  }

  while(cin>>n && n){
    set<int> stt;
    while(n--){
      cin >> p >> m;
      s = p-m;
      if(s < 0){
	s = 0;
      }
      t = p+m;
      if(t > 999983){
	t = 999983;
      }
      
      for(int i = 0; i < vec.size() && vec[i] <= t; i+=1){
	if(s <= vec[i] && vec[i] <= t){
	  stt.insert(vec[i]);
	}
      }
    }

    cout << ((stt.size() > 1)? (stt.size()-1): 0) << endl;
  }
  
  return 0;
}