#include <iostream>
#include <string.h>
#include <vector>
#include <algorithm>
using namespace std;
#define LIM 1000000
 
bool is_prime[LIM+10];
 
int main(){
    memset(is_prime, true, sizeof(is_prime));
    is_prime[0] = is_prime[1] = false;
 
    for(int p=2;p*p<=LIM;p++)if(is_prime[p]){
        for(int k=p*p;k<=LIM;k+=p)is_prime[k] = false;
    }
 
    vector<int> primes;
    for(int i=2;i<=LIM;i++)if(is_prime[i])primes.push_back(i);
 
    int n;
    while(cin >> n && n){
        int pay = 0;
        for(int i=0;i<n;i++){
            int p, m;
            cin >> p >> m;
 
            pay += upper_bound(primes.begin(), primes.end(), p + m) - lower_bound(primes.begin(), primes.end(), p - m) -  1;
        }
 
        cout << pay << endl;
    }
    return 0;
}