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++: #define _CRT_SECURE_NO_WARNINGS // #pragma warning(disable:4996)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <sstream>
#include <cmath>
#include <set>
#include <map>
#include <stack>
using namespace std; 

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(a);i>=(b);i--)
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define all(a) a.begin(),a.end()
#define len(x) ((int)(x).size())
#define tmax(a,b,c) max((a),max((b),(c)))
#define tmin(a,b,c) min((a),min((b),(c)))
#define debug(x) cerr << #x << " is " << x << endl;

typedef pair<int, int> Pii;
typedef map<int, int> Mii;
typedef vector<int> Vi;
typedef long long ll;
const int inf = 2e9;
const ll ll_inf = 1e17;
const int mod = 1e9 + 7;
const long double eps = 1e-10;

int p[1322501];

int main()
{
	rep(i,2,1150) {
		int j = i;
		while (j+i < 1322500) {
			j += i;
			p[j]++;
		}
	}

	while (1) {
		int n; cin>>n;
		if (!n) break;
		if (!p[n]) cout << 0 << endl;
		else {
			int l=n, r=n;
			while (p[l]) l--;
			while (p[r]) r++;
			cout << r-l << endl;
		}
	}
	return 0;
}

C++: /* prime_table.cpp
    エラトステネスの篩で素数かどうかの情報を保存
*/
#include <bits/stdc++.h>
using namespace std;

/*  prime_table(n)
    入力：整数 n
    出力：nまでの数字が素数か判定したテーブル
    計算量：O(nloglogn)
*/
vector<bool> prime_table(int n) {
    vector<bool> prime(n + 1, true);
    if (n >= 0) prime[0] = false;
    if (n >= 1) prime[1] = false;
    for (int i = 2; i * i <= n; i++) {
        if (!prime[i]) continue;
        for (int j = i * i; j <= n; j += i) {
            prime[j] = false;
        }
    }
    return prime;
}

int main() {
    vector<bool> p_table = prime_table(2000000);
    int N;
    while (cin >> N) {
        if (N == 0) break;
        int pre = N, next = N;
        while (!p_table[pre]) pre--;
        while (!p_table[next]) next++;
        cout << next - pre << endl;
    }
}

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

int main(){
  vector<bool> prime(1000001,1);
  prime[1]=0;
  for(int i=2;i<prime.size();i++){
    if(prime[i]==1){
      for(int j=i*2;j<prime.size();j+=i)prime[j]=0;
    }
  }
  //for(int i=0;i<prime.size();i++)if(prime[i]==1)cout<<i<<" ";
  while(1){
    int n;
    cin>>n;
    if(n==0)break;
    int ans=0;
    for(int i=0;i<prime.size();i++){
      if(prime[n+i]==1)break;
      ans++;
    }
    for(int i=0;i<prime.size();i++){
      if(prime[n-i]==1)break;
      ans++;
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <math.h>
using namespace std;
#define MAX 1299709

int isPrime(int a){
  int i;
  if (a==2) return 1;
  if (a%2==0) return 0;
  for (i=3;i<=sqrt(a);i+=2){
    if (a%i==0) return 0;
  }
  return 1;
}

int main(){
  int n,a,b;
  cin >> n;
  while (n!=0){
    a=0;
    b=0;
    if (isPrime(n)) cout << 0 << endl;
    else{
      for (int i=1;;i++){
	if (a==0) if (isPrime(n-i))a=n-i;
	if (b==0) if (isPrime(n+i))b=n+i;
	if (a!=0&&b!=0) break;
      }
      cout << b-a << endl;
    }
    cin >> n;
  }
  return 0;
}

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

#define max 1299709
using namespace std;

int main(void){
	//FILE* fp_in = freopen("data.txt", "r", stdin);

	//■■■■素数テーブル作成(エラトステネスのふるい)■■■■
	vector<bool> pt;
	for(int i = 0; i <= max; i++)
		pt.push_back(true);

	pt.at(0) = false;
	pt.at(1) = false;

	for(int i = 2; i <= max; i++)
		for(int j = i * 2; j <= max; j += i)
			pt.at(j) = false;

	//■■■■素数リスト作成■■■■
	vector<int> plist;
	for(int i = 0; i <= max; i++)
		if( pt.at(i) == true ) plist.push_back( i );


	while(1) {
		int n;
		cin >> n;
		if( n == 0 ) break;


		//n以下で最大の素数を調べる
		int p;
		for(int i = 0; i < plist.size() && plist.at(i) <= n; i++)
			p = plist.at(i);

		if( pt.at(n) == true ) {
			cout << 0 << endl;
		} else {
			int ans = 1;
			for(int i = p + 1; pt.at(i) == false; i++) 
				ans ++;
			cout << ans << endl;
		}
	}

	//while(1){}

	return 0;
}

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

using namespace std;

const int N = 1299709;

vector<bool> p;

void init(){
    p.assign(N, true); p[0] = p[1] = false;
    for(int i=2; i<=N; ++i)if(p[i]){
        for(int j=i<<1; j<=N; j+=i)p[j] = false;
    }
}

int main(){
    init();
    for(int k; cin >> k, k;){
        int lo = k, hi = k;
        while(!p[lo])lo--;
        while(!p[hi])hi++;
        cout << hi - lo << '\n';
    }
    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

int const MAX = 1299709;
int prime[MAX + 1] = {0}; //0::prime 1::NOTprime;

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

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    hurui();
    int n;
    //for(int i=1;i<=30;i++) cout << prime[i] << endl;
    while (cin >> n, n)
    {
        int res = 0;
        if (prime[n] == 0)
        {
            cout << res << endl;
        }
        else
        {
            int mae = n, ato = n + 1;
            while (prime[mae] == 1)
            {
                res++;
                mae--;
            }
            while (prime[ato] == 1)
            {
                res++;
                ato++;
            }
            cout << res + 1 << endl;
        }
    }
}