#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;

#define all(c) (c).begin(), (c).end()
const int N = 1299709;

template<typename T> inline T sqr(T x){return x * x;}

void sieve(vector<int>& v, int n){
    v.assign(n, 0); iota(all(v), 2);
    for(auto div=v.begin(); sqr(*div)<=v.back(); ++div){
        v.erase(remove_if(div+1, v.end(), [&](int x){return x % *div == 0;}), v.end());
    }
}

int main(){
    vector<int> primes;
    sieve(primes, N);
    for(int k; cin >> k, k;){
        auto itr = equal_range(all(primes), k);
        cout << (*itr.second == *itr.first? *itr.second - *(--itr.first): 0) << '\n';
    }
    return 0;
}