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++: //http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1276
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <queue>
using ll = long long;
using namespace std;

int const MOD = 1e9 + 7;
bool sieve[2000000];
int main(void)
{
    for (int i = 0; i < 2000000; ++i)
        sieve[i] = true;
    sieve[0] = sieve[1] = false;
    for (int i = 2; i * i < 2000000; ++i)
    {
        if (sieve[i])
        {
            for (int j = 2; i * j < 2000000; ++j)
            {
                sieve[i * j] = false;
            }
        }
    }
    int n;
    while (cin >> n, n)
    {
        int cnt = 0;
        for (int i = n;; --i)
        {
            if (sieve[i])
                break;
            cnt++;
        }
        for (int i = n;; ++i)
        {
            if (sieve[i])
                break;
            cnt++;
        }
        if (sieve[n])
            cout << 0 << endl;
        else
            cout << cnt << endl;
    }
    return 0;
}

C++: #include<iostream>
using namespace std;
bool n[2250000]={false};
int main(){
  for(int i=2; i<=1500; i++)
    if(!n[i])
      for(int j=i*i; j<2250000; j+=i)      
	n[j] = true;
  int m;
  while(cin >>m,m){
    int i,j;
    for(i=m;n[i];i--);
    for(j=m;n[j];j++);
    cout <<j-i<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main() {
	vector<bool> isprime(1300000, true);
	isprime[0] = isprime[1] = false;
	vector<int> prime;
	for (long long i = 2; i < 1300000; ++i) {
		if (isprime[i]) {
			prime.push_back(i);
			for (long long j = i * i; j < 1300000; j += i) isprime[j] = false;
		}
	}
	int k;
	while (cin >> k, k) {
		auto it = lower_bound(prime.begin(), prime.end(), k);
		if (*it == k) cout << 0;
		else cout << *it - *(it - 1);
		cout << endl;
	}

	return 0;
}

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

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
const int MAX_N =1299709;
int prime[MAX_N];//i番目の素数
bool is_prime[MAX_N+1];//is_prime[i]がtrueならiは素数

//n以下の素数の数を返す
int sieve(int n){
	int p = 0;
	for(int i = 0;i <=n;i++){
		is_prime[i] =true;
	}
	is_prime[0] = false;
	is_prime[1] = false;
	for(int i = 2;i <=n;i++){
		if(is_prime[i]){
			prime[p++] = i;
			for(int j = 2*i;j <=n;j +=i){
				is_prime[j] =false;
			}
		}
	}
	return p;
}
int main(){
	sieve(1299709);
	int N;
	int a1;
	int a2;
	while(cin>>N,N){
		a1 =N;
		a2 =N;
		for(int i = N;i >=0;i--){
			if(is_prime[i]){
				a1 =i;
				break;
			}
		}
		for(int i = N;i <=1299709;i++){
			if(is_prime[i]){
				a2 =i;
				break;
			}
		}
		cout <<a2-a1<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <numeric>
#include <string>
#include <string.h>
#include <map>
#include <set>
#include <functional>
#include <complex>

using namespace std;

const int INF = (1<<30) - 1;

bool is_prime[1299710];

void sieve(){
    memset(is_prime, true, sizeof(is_prime));
    is_prime[0] = is_prime[1] = false;
    for(int i=0; i<1299710; i++){
        if(is_prime[i]){
            for(int j=i*2; j<1299710; j+=i){
                is_prime[j] = false;
            }
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    sieve();
    
    int N;
    while(cin>>N, N){
        if(is_prime[N]){
            cout << 0 << endl;
        }else{
            int r, l;
            r = l = N;
            while(!is_prime[r]) r--;
            while(!is_prime[l]) l++;
            cout << l-r << endl;
        }
    }
    
    return 0;
}