#include <iostream>
#include <vector>
#include <cstring>
using namespace std;

int main(){
  bool compnum[1299710];
  memset(compnum, false, sizeof(compnum));
  for (int i = 2; i*i <= 1299709; i++) {
    for (int j = i+i; j <= 1299709; j+=i) {
      compnum[j] = true;
    }
  }
  while (true) {
    int x;
    cin >> x;
    if (x == 0) break;
    int l, r;
    l = r = x;
    while (l > 2 && compnum[l]) l--;
    while (r < 1299709 && compnum[r]) r++;
    cout << r-l << endl;
  }
}