#include<bits/stdc++.h>
using namespace std;
 
#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)
#define SORT(c) sort((c).begin(),(c).end())
#define IINF INT_MAX
#define LLINF LLONG_MAX
#define DEBUG false

typedef long long ll;
typedef pair<int, int> ii;

vector<int> prime;

int main() {
	
	vector<bool> use(1299710, false);
	REP(i, 2, 1299710){
		if(!use[i])prime.push_back(i);
		for(int j = 2; i * j <= 1299710; j++) use[i * j] = true;
	}
	
	int n;
	while(cin >> n, n){
		auto it1 = upper_bound(prime.begin(), prime.end(), n);
		auto it2 = lower_bound(prime.begin(), prime.end(), n);
		it1--;
		cout << *it2 - *it1 << endl;
	}
	
	
	return 0;
}