#include <bits/stdc++.h>
using namespace std;
const int SIZE=2000000;
bool is_prime[SIZE];
int main() {
    memset(is_prime,-1,sizeof(is_prime));
    for(int i=2;i*i<SIZE;i++) {
        if(!is_prime[i]) continue;
        for(int j=i*2;j<SIZE;j+=i) is_prime[j]=0;
    }
    vector<int> prime;
    for(int i=2;i<SIZE;i++) if(is_prime[i]) prime.push_back(i);
    int n;
    while(scanf("%d",&n),n) {
        int pos=lower_bound(prime.begin(),prime.end(),n)-prime.begin();
        if(prime[pos]==n) {printf("0\n");continue;}
        printf("%d\n",prime[pos]-prime[pos-1]);
    }
}