#include<iostream>
using namespace std;
#include<cmath>
bool isprime(int n) {
	if (n == 2)return true;
	if (n % 2 == 0)return false;
	for (int i = 3; i <= sqrt(n); i += 2) if (n%i == 0)return false;
	return true;
}
int main() {
	int n;
	while (cin >> n, n) {
		if (isprime(n))cout << "0\n";
		else {
			int r, l;
			for (r = n - 1; !isprime(r); r--);
			for (l = n + 1; !isprime(l); l++);
			cout << l - r << endl;
		}
	}
}