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

typedef long long ll;

vector<ll> prime;

int main(){
  prime.push_back(2);
  for(ll i=3; i<1300000; i+=2){
    bool flag=true;
    for(ll j=0; j<prime.size() && i>=prime[j]*prime[j]; j++){
      if(i%prime[j]==0){flag=false; break;}
    }
    if(flag)prime.push_back(i);
  }
  while(true){
    int n;
    cin>>n;
    if(n==0)break;
    ll to;
    for(to=0; n>prime[to]; to++);
    if(n==prime[to])cout<<"0"<<endl;
    else cout<<prime[to]-prime[to-1]<<endl;
  }
  return 0;
}