#include <bits/stdc++.h>

using namespace std;

const int MAX = 999984;
vector<int> v;
bool prime[MAX];

void sieve(){
    fill(prime, prime+MAX, true);
    prime[0] = prime[1] = false;

    for(int i=0; i < MAX; i++){
        if(!prime[i]) continue;
        v.push_back(i);
        for(int j=i+i; j < MAX; j += i){
            prime[j] = false;
        }
    }
}

int main(){
    sieve();
    int n;
    while(cin >> n, n){
        int ans = 0;
        for(int i=0; i< n; i++){
            int a, b;
            cin >> a >> b;
            int p = lower_bound(v.begin(), v.end(), a+b+1) - lower_bound(v.begin(), v.end(), a-b);
            ans += p;
        }
        ans -= n;
        cout << ans << endl;
    }
}