#include<iostream>
#include<cmath>
using namespace std;
int X, Y, A, i, j;
int main() {
	while (true) {
		cin >> A; if (A == 0) { break; }
		for (i = A; i >= 2; i--) { for (j = 2; j <= sqrt(i); j++) { if (i%j == 0) { goto E; } }X = i; goto F; E:; }
	F:; for (i = A; i < 99999999; i++) { for (j = 2; j <= sqrt(i); j++) { if (i%j == 0) { goto G; } }Y = i; goto H; G:; }
	H:; cout << Y - X << endl;
	}
}