#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
#define MAX 1000000
bool is_prime[MAX+1];
vector<int> prime;
void init() {
    memset(is_prime, 1, sizeof(is_prime));
    is_prime[0] = is_prime[1] = false;
    for (int i = 2; i <= MAX; i++) 
        if (is_prime[i]) {
            prime.push_back(i);
            for (int j = i + i; j <= MAX; j += i) 
                is_prime[j] = false;
        }
}

int cnt(int p, int m) {
    typedef vector<int>::iterator vi;
    vi u = lower_bound(prime.begin(), prime.end(), p+m+1);
    vi l = lower_bound(prime.begin(), prime.end(), p-m);
    return u - l;
}

int main() {
    int n, p, m;
    init();
    while (cin >> n, n) {
        int sum = 0;
        while (n--) {
            cin >> p >> m;
            int prize = cnt(p, m) - 1;
            sum += prize;
        }
        cout << sum << endl;
    }
    return 0;
}