#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

const int MAX = 1000000;
const int MP = 999983;
int n;
bool is_prime[(MAX + 1) * 2 + 1];
int prime_count[(MAX + 1) * 2 + 1];

void sieve()
{
    for(int i = 0; i <= (MAX + 1) * 2; i++) 
        is_prime[i] = false;

    is_prime[2] = true;
    for(int i = 3; i <= MP; i+=2) {
        is_prime[i] = true;
        for(int j = 3; j*j <= i; j+=2) {
            if(i % j == 0) {
                is_prime[i] = false;
                break;
            }
        }
    }
}

void calc_count()
{
    prime_count[0] = 0;
    for(int i = 1; i <= (MAX + 1) * 2; i++) {
        prime_count[i] = prime_count[i-1];
        if(is_prime[i])
            prime_count[i]++;
    }
}

int main()
{
    sieve();
    calc_count();
    while(cin >> n && n) {
        int p,m,sum,x;
        sum = 0;
        for(int i = 0; i < n; i++) {
            cin >> p >> m;
            int lb, ub;
            lb = (p - m - 1 >= 0) ? p - m - 1 : 0;
            ub = p + m;
            x = prime_count[ub] - prime_count[lb];
            sum += x - 1;
        }
        cout << sum << endl;
    }
}