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 <bits/stdc++.h>

using namespace std;

// エラトステネスの篩
vector<bool> makeJudgementTable(int64_t number) {
    vector<bool> prime(number + 1, true);
    if (number >= 0) prime[0] = false;
    if (number >= 1) prime[1] = false;
    for (int64_t i = 2; i <= number; i++) {
        if (!prime[i]) continue;
        for (int64_t j = i * i; j <= number; j = j + i) {
            prime[j] = false;
        }
    }
    return prime;
}

int main() {
    vector<int64_t> N;

    while (true) {
        int tmp;
        cin >> tmp;
        if (tmp == 0) break;
        N.push_back(tmp);
    }

    const int64_t MAX_NUMER = 1299709;
    vector<bool> isPrimeNumbers = makeJudgementTable(1299709);

    // kが素数かどうか判断する
    // 素数の場合 -> 0
    // 素数でない場合 -> 前後の素数を求める -> 前後の素数の差を計算する

    vector<int64_t> primeGaps(N.size(), 0);

    for (int i = 0; i < N.size(); i++) {
        if (isPrimeNumbers.at(N.at(i))) {  // 素数の時
            primeGaps.at(i) = 0;
        } else {  // 素数ではない時

            int64_t previousPrimeNumber = 0;  // 前の素数
            for (int64_t j = N.at(i); 0 < j; j--) {
                if (isPrimeNumbers.at(j)) {
                    previousPrimeNumber = j;
                    break;
                }
            }
            int64_t followingPrimeNumber = 0;  // 後ろの素数
            for (int64_t j = N.at(i); j <= MAX_NUMER; j++) {
                if (isPrimeNumbers.at(j)) {
                    followingPrimeNumber = j;
                    break;
                }
            }
            primeGaps.at(i) = followingPrimeNumber - previousPrimeNumber;
        }
    }

    for (int64_t i = 0; i < primeGaps.size(); i++) {
        cout << primeGaps.at(i) << endl;
    }

    return 0;
}

C++: #include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
vector<int> P;
const int pmax=1299709;
bool iscomp[pmax+1];
void init()
{
	P.reserve(100000);
	for(int i=2;i<pmax+1;i++){
		if(iscomp[i])continue;
		P.push_back(i);
		for(int j=2;i*j<pmax+1;j++)
			iscomp[i*j]=true;
	}
}
int main()
{
	init();
	while(true){
		int n;
		cin>>n;
		if(n==0)return;
		int pos=lower_bound(n)-R.begin();
		if(P[pos]==n)cout<<0<<endl;
		else{
			cout<<P[pos+1]-P[pos]<<endl;
		}
	}
}

C++: #include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


namespace{
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}


#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;

const int maxn=100000000;
int f[maxn],prime[maxn];
 
int GenP() {
	int i,tot,p,tmp;
	tot=0;
	for (i=2;i<maxn;++i) f[i]=i;
	for (p=2;p<maxn;++p)
	{
		if (f[p]==p) prime[tot++]=p;
		for (i=0;i<tot && prime[i]<=f[p];++i)
		{
			tmp=prime[i]*p;
			if (tmp<maxn) 
				f[tmp]=prime[i];
			else
				break;
		}
	}
}


void mainmain(){
	GenP();
	set<int> se;
	rep(i,1000003){
		se.insert(prime[i]);
	}
	int n;
	while(cin>>n,n){
		
		auto it1=se.lower_bound(n);
		auto it2=se.upper_bound(n);
		if(*it1!=*it2){
			cout<<0<<endl;
			continue;
		}
		it2--;
		if(*it1==*it2){
			cout<<0<<endl;
		}
		else{
			cout<<*it1-*it2<<endl;
		}
	}
}



}
main() try{
    mainmain();
}
catch(...){
}