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

const int MP = 999983;
int n;

bool is_prime(int num)
{
    if(num > MP) return false;
    if(num == 2) return true;
    if(num % 2 == 0) return false;

    bool ret = true;
    for(int i = 3; i*i <= num; i+=2) {
        if(num % i == 0) {
            ret = false;
            break;
        }
    }
    return ret;
}

int main()
{
    while(cin >> n && n) {
        int p,m,sum,x;
        sum = 0;
        for(int i = 0; i < n; i++) {
            cin >> p >> m;
            x = 0;
            for(int a = p - m; a <= p + m; a++) {
                if(a <= 1) continue;
                if(is_prime(a)) 
                    x++;
            }
            sum += x - 1;
        }
        cout << sum << endl;
    }
}