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

using namespace std;

int main() {
	vector<bool> isprime(1300000, true);
	isprime[0] = isprime[1] = false;
	vector<int> prime;
	for (long long i = 2; i < 1300000; ++i) {
		if (isprime[i]) {
			prime.push_back(i);
			for (long long j = i * i; j < 1300000; j += i) isprime[j] = false;
		}
	}
	int k;
	while (cin >> k, k) {
		auto it = lower_bound(prime.begin(), prime.end(), k);
		if (*it == k) cout << 0;
		else cout << *it - *(it - 1);
		cout << endl;
	}

	return 0;
}
