#include <iostream>
#include <string>
#include <vector>
#include <cstring>
using namespace std;

const int MP = 999983;
bool sieve[MP+1];
int ac[MP+1];

void makeSieve() {
    memset(sieve, false, sizeof sieve);
    sieve[0] = sieve[1] = true;
    for (int i=2; i*i<=MP; ++i) {
        if (sieve[i]) continue;
        for (int j=i*i; j<=MP; j+=i) {
            sieve[j] = true;
        }
    }
}

void acum() {
    ac[0] = 0;
    for (int i=1; i<=MP; ++i) {
        ac[i] = ac[i-1] + !sieve[i];
    }
}

int main() {
    makeSieve(); acum();
    int n, p, m;
    while (cin >> n, n) {
        int ret = 0;
        for (int i=0; i<n; ++i) {
            cin >> p >> m;
            int l = max(0, p-m);
            int h = min(MP, p+m);
            int X = ac[h] - (l-1 < 0 ? 0 : ac[l-1]);
            ret += X - 1;
        }
        cout << ret << endl;
    }
    return 0;
}