#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,j) FOR(i,0,j)
#define mp std::make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int,int> P;
typedef std::pair<int,P> State;

const int INF = 1001001001;

// S N E W(南北東西)
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

std::vector<int> primes;
bool isPrime[1000000 + 1];

void initPrimes(){
    std::fill(isPrime, isPrime+1000000+1, true);

    isPrime[0] = false;
    isPrime[1] = false;
    for(int i=4;i<=1000000;i+=2){isPrime[i] = false;}
    
    primes.push_back(2);

    for(int i=3;i<=1000000;i+=2){
        if(isPrime[i]){
            primes.push_back(i);
            for(int j=2*i;j<=1000000;j+=i){
                isPrime[j] = false;
            }
        }
    }
}

int main(){
    initPrimes();

    int N;
    while(std::cin >> N, N){
        int res = 0;
        REP(i, N){
            int P, M;
            std::cin >> P >> M;
            res += std::upper_bound(primes.begin(), primes.end(), P+M)
                - std::lower_bound(primes.begin(), primes.end(), P-M) - 1;
        }

        std::cout << res << std::endl;
    }
}