#include <iostream>
using namespace std;

int main(){
	char est[1300000];
	for(int i = 2; i < 1300000; i++){
		est[i] = '1';
	}
	for(int i = 2; i * i <= 1300000; i++){
		for(int k = 2; k * i < 1300000; k++){
			est[i*k] = '0';
		}
	}
	int n;
	while(cin >> n){
		if(n == 0) break;
		int i = n, j = n;
		while(est[i] == '0') i--;
		while(est[j] == '0') j++;
		cout << j - i << endl;
	}
	return 0;
}