#include <iostream>

using namespace std;

const int max_p = 1300000;
bool prime[max_p + 1];

void initial_prime()
{
    for (int i = 0; i < max_p; i++)
        prime[i] = true;
    prime[0] = prime[1] = false;

    for (int i = 2; i * i < max_p; i++)
        if (prime[i])
            for (int j = 2 * i; j <= max_p; j += i)
                prime[j] = false;
}

int main()
{
    int k;
    
    initial_prime();

    while (cin >> k, k) {
        int l, r;
        int ans = 0;

        l = r = k;
        while (!prime[l--])
            ans++;
        while (!prime[r++])
            ans++;
        cout << ans << endl;
    }

    return 0;
}