#include <iostream>

using namespace std;
using ll = long long;

inline bool isprime(ll n)
{
    for (ll i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

int main()
{
    while (true) {
        ll n;
        cin >> n;
        if (n == 0) {
            break;
        }
        ll inf = n;
        for (;; inf--) {
            if (isprime(inf)) {
                break;
            }
        }
        ll sup = n;
        for (;; sup++) {
            if (isprime(sup)) {
                break;
            }
        }
        cout << sup - inf << endl;
    }

    return 0;
}