#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

void makePrimeNumber(int N, vector<bool>& prime)
{
    prime.assign(N+1, true);
    prime [0] = prime[1] = false;
    for(int i=2; i*i<=N; i++){
        if(prime[i]){
            for(int j=i; i*j<=N; j++){
                prime[i*j] = false;
            }
        }
    }
}

int main()
{
    const int MAX = 1000000;

    vector<bool> prime;
    makePrimeNumber(MAX, prime);

    vector<int> num(MAX+1, 0);
    for(int i=1; i<=MAX; ++i){
        num[i] = num[i-1];
        if(prime[i])
            ++ num[i];
    }

    for(;;){
        int n;
        cin >> n;
        if(n == 0)
            return 0;

        int ret = -n;
        for(int i=0; i<n; ++i){
            int p, m;
            cin >> p >> m;
            int a = max(1, p-m);
            int b = min(MAX, p+m);
            ret += num[b] - num[a-1];
        }
        cout << ret << endl;
    }
}