#define _CRT_SECURE_NO_WARNINGS
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <queue>
using namespace std;
typedef long long ll;
#define all(c) (c).begin(), (c).end()
#define loop(i,a,b) for(ll i=a; i<ll(b); i++)
#define rep(i,b) loop(i,0,b)
#ifdef DEBUG
#define dump(...) (cerr<<#__VA_ARGS__<<" = "<<(DUMP(),__VA_ARGS__).str()<<" ["<<__LINE__<<"]"<<endl)
struct DUMP:ostringstream{template<class T>DUMP &operator,(const T&t){if(this->tellp())*this<<", ";*this<<t;return *this;}};
#else
#define dump(...)
#endif

const int N=1000111;
bool isp[N];
vector<int> prime;
void init(){
    memset(isp, 1, sizeof(isp));
    isp[0] = isp[1] = false;
    for(int i=2;i<N;i++){
        if(!isp[i])continue;
        prime.push_back(i);
        for(int j=i+i;j<N;j+=i){
            isp[j] = false;
        }
    }
}

int cnt(int p, int m) {
    auto u = lower_bound(prime.begin(), prime.end(), p+m+1);
    auto l = lower_bound(prime.begin(), prime.end(), p-m);
    return u - l;
}

int n, p, m;

int main() {
    init();
    while(cin>>n && n) {
        int ans = 0;
        rep(x,n){
            cin >> p >> m;
            ans += cnt(p, m)-1;
        }
        cout << ans << endl;
    }
}