#include <iostream>
#include <cmath>
using namespace std;

int p[100010], size;

bool isPrime(int x){
	for(int i=0;i<size && p[i]<=sqrt(x);i++){
		if(x % p[i] == 0) return false;
	}
	return true;
}

int main(void){
	int n;

	p[0] = 2;
	size = 1;
	for(int i=3;i<1299730;i+=2) if(isPrime(i)) p[size++] = i;

	while(cin>>n && n){
		int i;
		for(i=0;p[i]<n;i++);

		if(p[i] == n) cout<<"0\n";
		else cout<<p[i] - p[i-1]<<endl;
	}

	return 0;
}