#include<bits/stdc++.h>
using namespace std;

int prime[999985];
int main()
{
  fill_n(prime, 999985, 1);
  prime[1] = 0;
  for(int i = 2; i * i <= 999985; i++) {
    if(prime[i]) {
      for(int j = i + i; j <= 999985; j += i) {
        prime[j] = 0;
      }
    }
  }
  for(int i = 1; i <= 999985; i++) {
    prime[i] += prime[i - 1];
  }

  int n;
  while(scanf("%d", &n), n) {
    long long ret = 0LL;
    for(int i = 0; i < n; i++) {
      int p, m;
      scanf("%d %d", &p, &m);
      int low = max(1, p - m);
      int high = min(999984, p + m);
      ret += prime[high] - prime[low - 1] - 1;
    }
    printf("%d\n", ret);
  }
}