#include <bits/stdc++.h>

using namespace std;

#define MP 999983

bool is_prime[MP+1];

int s_prime[MP+1];

int count_prime(int l, int r) {
    if(l<=0) l=1;
    if(r>MP) r=MP;
    return s_prime[r] - s_prime[l-1];
}

int main() {
    fill(&is_prime[2], &is_prime[MP+1], true);
    for(int p=2; p*p<=MP; p++) {
        if(is_prime[p]) {
            for(int i=p*p; i<=MP; i+=p) {
                is_prime[i] = false;
            }
        }
    }
    for(int i=1; i<=MP; i++) s_prime[i] = s_prime[i-1] + (is_prime[i] ? 1 : 0);
    
    int n;
    while(cin>>n, n) {
        int res = 0;
        for(int i=0; i<n; i++) {
            int p, m;
            cin >> p >> m;
            res += count_prime(p-m, p+m) - 1;
        }
        cout << res << endl;
    }
    
    return 0;
}


