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

const int MAX = 999984;
bool isPrime[MAX];

void createIsPrime() {
    fill_n(isPrime, MAX, true);
    isPrime[0] = false;
    isPrime[1] = false;
    for (int i = 4; i < MAX; i += 2) {
        isPrime[i] = false;
    }
    for (int i = 3; i * i <= MAX; i += 2) {
        if (isPrime[i]) {
            for (int j = i * 2; j < MAX; j += i) {
                isPrime[j] = false;
            }
        }
    }
}

int main() {
    createIsPrime();
    while (true) {
        int n;
        cin >> n;
        if (n == 0) {
            break;
        }
        int bill = 0;
        while (n--) {
            int p, m;
            cin >> p >> m;
            int prime = 0, start = p - m;
            if ((p - m) % 2 == 0) {
                start++;
            }
            if (p - m <= 2) {
                bill++;
            }
            for (int i = max(start, 3), end = min(p + m, MAX - 1); i <= end; i += 2) {
                if (isPrime[i]) {
                    prime++;
                }
            }
            if (prime > 0) {
                bill += prime - 1;
            } else {
                bill--;
            }
        }
        cout << bill << endl;
    }
}