import std.stdio;
import std.algorithm;
import std.string;
import std.range;
import std.array;
import std.conv;
import std.complex;
import std.math;
import std.ascii;
import std.bigint;
import std.container;
import std.typecons;

auto readInts() {
	return array(map!(to!int)(readln().strip().split()));
}
auto readInt() {
	return readInts()[0];
}
auto readLongs() {
	return array(map!(to!long)(readln().strip().split()));
}
auto readLong() {
	return readLongs()[0];
}

const real eps = 1e-10;

void main(){
	auto notPrime = new bool[](2000000);
	notPrime[0] = notPrime[1] = true;
	for(int i; i < notPrime.length; ++i) {
		if(!notPrime[i]) {
			for(int j = 2; i*j < notPrime.length; ++j) {
				notPrime[i*j] = true;
			}
		}
	}
	int[] primes;
	for(int i; i < notPrime.length; ++i) {
		if(!notPrime[i]) {
			primes ~= i;
		}
	}
	while(true) {
		int k = readInt();
		if(k == 0) return;
		auto t = primes.assumeSorted.trisect(k);
		if(!t[1].empty) {
			writeln(0);
		} else {
			writeln(t[2][0] - t[0][$-1]);
		}
	}
}