#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
using namespace std;

const int SIZE = 1000001;
int T[SIZE];
bool P[SIZE];

int solve( int p, int m ) {
    int left = max( 1, p - m );
    int right = min( SIZE - 1, p + m );
    int t = T[right] - T[left-1];
    return t == 0 ? -1 : t - 1;
}

int main() {
    for ( int i = 0; i < SIZE; ++ i ) {
        T[i] = 0;
        P[i] = false;
    }
    for ( int i = 3; i < SIZE; i += 2 ) {
        P[i] = true;
    }
    P[2] = true;
    for ( int i = 3; i < SIZE; i += 2 ) {
        if ( ! P[i] ) continue;
        for ( int j = i + i; j < SIZE; j += i ) {
            P[j] = false;
        }
    }
    for ( int i = 1; i < SIZE; ++ i ) {
        T[i] = T[i-1];
        if ( P[i] ) T[i] ++;
    }
    
    int n;
    while ( cin >> n && n ) {
        int res = 0;
        for ( int i = 0; i < n; ++ i ) {
            int p, m;
            cin >> p >> m;
            res += solve( p, m );
        }
        cout << res << endl;
    }
    return 0;
}