#include <iostream>
#include <vector>
#include <cmath>
#include <string>
#include <algorithm>
#include <iomanip>
#include <map>
#include <bitset>
#include <cstdio>
#include <set>
#include <stack>
#include <queue>
#include <cassert>
#include <numeric>
#define rep(i,n) Rep(i,0,n)
#define Rep(i,k,n) for(int i=k ; i<n ; i++)
#define rep1(i,n) for(int i=1 ; i<=n ; i++)
#define Rep1(i,k,n) for(int i=k ; i<=n ; i++)
#define rrep(i,n) for(int i=n ; i>=0 ; i--)
#define vi vector<int>
#define vii vector<vector<int>>
#define mii map<int,int>
#define Sort(v) sort(v.begin(),v.end())
#define Reverse(v) reverse(v.begin(),v.end())
#define ALL(a)  (a).begin(),(a).end()
#define pb push_back
#define mp make_pair
//#define int ll
typedef long long ll;

const int md = 1000000007;
const int INF = 1<<30;
using namespace std;

//vi prime(1000000,0);
vector<bool> isp;

void makePrime(int n){
    isp.assign(n+1,true);
    int id = 0;
    for(int i=2 ; i <= n ; i++){
        if(!isp[i]) continue;

        for(int k = i*2 ; k <= n ; k += i){
            isp[k] = false;
        }
        /*
        prime[id] = i;
        id++;
        */
        
        //prime.pb(i);
    }
    //Sort(prime);
}

ll check(int a, int b){
    ll ans = 0;
    if(a-b <= 2) ans++;
    int s;
    if((a-b)%2 == 0) s = a-b+1;
    else s = a - b;

    for(int i = max(s,3); i <= min(a+b,999983) ; i += 2) {
        if(isp[i]) ans++;
    }
    return ans;
}
int main(){
    makePrime(999983);
    //cout << prime.size() << endl;
    //cout << binary_search(ALL(prime),2) << endl;
    int n; 
    while(true){
        cin >> n;
        if(n == 0) break;
        ll ans = 0;
        rep(i,n){
            int a,b; cin >> a >> b;
            //cout << check(a,b) << endl;
            int tmp = check(a,b);
            //cout << "check: " <<  tmp << endl;
            ans += check(a,b)-1;
            
        }
        cout << ans << endl;
    }
}

