Go: package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
	"strings"
)

func main() {
	io := NewIo()
	defer io.Flush()
	isPrime := Sieve(1299709)
	for {
		k := io.NextInt()
		if k == 0 {
			break
		}
		ans := 0
		if !isPrime[k] {
			p, n := k-1, k+1
			for !isPrime[p] || !isPrime[n] {
				if !isPrime[p] {
					p--
				}
				if !isPrime[n] {
					n++
				}
			}
			ans = n - p
		}
		io.Println(ans)
	}
}

func Sieve(x int) []bool {
	flags := make([]bool, x+1)
	for i := 2; i <= x; i++ {
		flags[i] = true
	}
	for i := 2; i <= int(math.Sqrt(float64(x))); i++ {
		if !flags[i] {
			continue
		}
		for j := i * i; j <= x; j += i {
			flags[j] = false
		}
	}
	return flags
}

// I/O template
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buff []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buff = append(buff, line...)
		if !isPrefix {
			break
		}
	}
	return string(buff)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	s := io.tokens[io.nextToken]
	io.nextToken++
	return s
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntSlice(a []int) {
	b := make([]interface{}, 0, len(a))
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := make([]interface{}, 0, len(a))
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func ExampleIo() {
	io := NewIo()
	defer io.Flush()
	a := make([]int, 0, 3)
	a[0], a[1], a[2] = io.NextInt(), io.NextInt(), io.NextInt()
	io.Println(a)
	io.PrintIntSlice(a)
	Log("io", a)
}

C++: #include <iostream>
#include <vector>
using namespace std;
#define MAX 1300000

vector<int> prime(MAX);

void mk_prime(){
    for( int i = 0; i < MAX; i++){
        prime[i] = 1;
    }
    prime[0] = 0;
    prime[1] = 0;
    for( int i = 2; i < MAX; i++ ){
        if( prime[i] == 1 ){
            for( int j = 2 * i; j < MAX; j += i ){
                prime[j] = 0;
            }
        }
    }
}

int main(){
    mk_prime();
    int k;

    while( 1 ){
        cin >> k;
        if( k == 0 ) return 0;
        if( prime[k] == 1 ){
            cout << 0 << endl;
            continue;
        }
        int s, e;
        for( int i = k - 1; ; i-- ){
            if( prime[i] == 1 ){
                s = i;
                break;
            }
        }
        for( int i = k + 1; ; i++ ){
            if( prime[i] == 1 ){
                e = i;
                break;
            }
        }
        int ans = e - s;
        cout << ans << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(){
	int n,a[100001]={1,2,0,0},c,i,j;
	for(i=4;i<100000;i++){
		for(j=2,c=0;j*j<=i;j++)if(i%j==0)c++;
		if(c)a[i]+=a[i-1]+1;
else a[i]=0;
	}
	for(i=99999;i>=0;i--){
		if(a[i]){for(j=0,c=a[i];j<c;j++)a[i--]=c+1;i++;}
	}
	while(cin>>n,n)cout<<a[n]<<endl;
}

C++: #include <iostream>

using namespace std;

bool prime_check(int n, int k = 2)
{
	if (n < k * k) { return true; }

	if (n % k == 0) { return false; }

	return prime_check(n, k + 1);
}

int main()
{
	int n;

	while (true)
	{
		cin >> n;

		if (n == 0) { break; }

		int l = n;
		int r = n;

		while (prime_check(l) == false)
		{
			l--;
		}

		while(prime_check(r) == false)
		{
			r++;
		}

		cout << r - l << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)n; ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cout<<*i<<" "; cout<<endl; }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;

bool is_prime(ll n){
	for(int i = 2; (ll)i * i <= n; i++){
		if(n % i == 0) return false;
	}
	return n != 1;
}

int main(){
	int n;
	while(cin>>n,n){
		if(is_prime(n))cout<<"0"<<endl;
		else{
			int ans = 0;
			for(int i = n - 1; i >= 2; i--){
				if(is_prime(i)){
					ans += abs(n - i);
					break;
				}
			}
			for(int i = n + 1; i <= INF; i++){
				if(is_prime(i)){
					ans += abs(n - i);
					break;
				}
			}
			cout<<ans<<endl;
		}
	}
	return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1 << 28;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

vector<bool> isprime;

void eratos(ll n) {
	isprime.resize(n + 1);
	for (ll i = 0; i <= n; i++) {
		isprime[i] = true;
	}
	isprime[0] = false;
	isprime[1] = false;
	for (ll i = 2; i * i <= n; i++) {
		if (isprime[i]) {
			ll j = i + i;
			while (j <= n) {
				isprime[j] = false;
				j = j + i;
			}
		}
	}
}
int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/
	int kkt = 89;
	eratos(MAX);
	vector<ll> pr; for (int i = 2; i < MAX; i++) if (isprime[i]) pr.push_back(i);
	while (kkt) {
		ll n; scanf("%lld", &n);
		if (n == 0) break;
		if (isprime[n]) puts("0");
		else {
			auto itr = upper_bound(pr.begin(), pr.end(), n);
			ll r = *itr;
			ll l = *prev(itr);
			printf("%lld\n", r - l);
		}
	}
	return 0;

}

C++: #include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <functional>
#include <set>
#include <sstream>
#include <map>
#include <queue>
 
using namespace std;
  
const double eps=1e-10;
 
const int dy[]={-1,-1,0,1,1, 1, 0,-1};
const int dx[]={ 0, 1,1,1,0,-1,-1,-1};
 
int main()
{
 
	vector<long> prime;
	vector<bool> is_prime(1299709+1,false);
	prime.push_back(2); is_prime[2]=true;
	prime.push_back(3); is_prime[3]=true;
	prime.push_back(5); is_prime[5]=true;
 
	for(int i=7;prime.back()<=1299709;i+=2){
		bool ok=true;
		for(int j=0;prime[j]*prime[j]<=i && ok;j++){
			if(i%prime[j]==0) ok=false;
		}
		if(ok){is_prime[i]=true; prime.push_back(i);}
//	  if(ok) cout<<i<<endl;
	}
 
	long k;
	while(cin>>k,k){
		long low,high;

		for(long i=k;;i--){
			if(is_prime[i]){low=i; break;}
		}
		for(long i=k;;i++){
			if(is_prime[i]){high=i; break;}
		}
		cout<<high-low<<endl;
	}
 
 
	return 0;
}