#include <stdio.h>

#define SIZE_F 1300000

int furui[SIZE_F];
int prime[100100];

int main() {
	int i, j, k;
	int n;

	for(i = 2, k = 0; i < SIZE_F; i++) {
		if(!furui[i]) {
			for(j = i * 2; j < SIZE_F; j += i) {
				furui[j] = 1;
			}
			prime[k] = i;
			k++;
		}
	}

	while(1) {
		scanf("%d", &n);
		if(!n)
			break;
		int middle;
		int top = k - 1, bottom = 0;

		while(top - bottom > 1) {
			middle = (top + bottom) / 2;
			
			if(prime[middle] == n)
				break;
			else if(prime[middle] > n)
				top = middle;
			else
				bottom = middle;
		}

		if(!furui[n])
			printf("0\n");
		else
			printf("%d\n", prime[top] - prime[bottom]);
	}

	return 0;
}