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<stdio.h>
#include<string.h>

char so[1299710];

int main(){
	
	int n,i,j,count;
	memset(so,0,sizeof(so));
	so[1]=1;
	for(i=2;i<6;i++){
		for(j=2*i;j<=29;j+=i){
			so[j]=1;
		}
	}
	
	for(i=29;i<1141;i+=2){
		if(i%23==0 ||i%7==0 || i%3==0 || i%5==0 || i%11==0 || i%13==0 || i%17==0 || i%19==0)continue;
		for(j=3*i;j<1299710;j+=(2*i)){
			so[j]=1;
		}
	}

	while(0<=scanf("%d",&n)){
		if(n==0)break;
		count=0;
		
		if(n==2 || n==3 || n==5 || n==7 || n==11 || n==13 || n==17 || n==23 || n==29){
			printf("0\n");
			continue;
		}
		
		for(i=n;;i--){
			if(i==2||i==3||i==5||i==7||i==11||i==17||i==19||i==23||i==29)break;
			if(i%2==0 ||i%23==0 ||i%7==0 || i%3==0 || i%5==0 || i%11==0 || i%13==0 || i%17==0 || i%19==0){
				++count;
				continue;
			}
			if(so[i]==0){
				break;
			}
			++count;
		}
		
		for(i=n;;i++){
			if(i==2||i==3||i==5||i==7||i==11||i==17||i==19||i==23||i==29)break;
			if(i%2==0 ||i%23==0 ||i%7==0 || i%3==0 || i%5==0 || i%11==0 || i%13==0 || i%17==0 || i%19==0){
				++count;
				continue;
			}
			if(so[i]==0){
				break;
			}
			++count;
		}
		
		
		
		printf("%d\n",count);

	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define vi vector<ll>
#define vvi vector<vi>
#define DBG_N(hoge) cerr<<" "<<(hoge)<<endl;
#define DBG cerr<<"!"<<endl;
#define BITLE(n) (1LL<<((ll)n))
#define BITCNT(n) (__builtin_popcountll(n))
#define SUBS(s,f,t) ((s).substr((f)-1,(t)-(f)+1))
#define ALL(a) (a).begin(),(a).end()
using namespace std;
bool pri[2222222];
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll k;
  vi prime;
  prime.pb(2);
  pri[2]=true;
  for(int i=3;i<=1299709;i++){
    bool hoge=true;
    for(int j=2;j<=sqrt(i);j++)if(i%j==0){hoge=false;break;}
    if(hoge){pri[i]=true;prime.pb(i);}
  }
  while(cin>>k,k){
    if(pri[k])cout<<0<<endl;
    else{
      auto lb=lower_bound(ALL(prime),k);
      cout<<(*lb-*(lb-1))<<endl;
    }
  }
  return 0;
}

C++: #pragma GCC optimize("O3")
#include<bits/stdc++.h> 
using namespace std;
using ll=long long;
using ull=unsigned long long int;
using P=pair<ll,ll>;
template<class T> using V=vector<T>; 
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
const ll inf=(1e18);
const ll mod=1000000007;
ll gcd(ll a,ll b) {return b ? gcd(b,a%b):a;}
ll lcm(ll c,ll d){return c/gcd(c,d)*d;}
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
vector<bool> prime;
void make(int num){
    prime.resize(num,true);
    prime[0]=false;
    for(int i=0;i*i<num;i++){
        if(prime[i]){
            for(int j=2;(i+1)*j<=num;j++){
                prime[(i+1)*j-1]=false;
            }
        }
    }
}
int main(){
make(1300000);
V<ll> prim;
for(int i=1;i<1300000;i++)if(prime[i])prim.emplace_back(i+1);
int m=prim.size();
while(1){
  ll n;
  cin>>n;
  if(n==0)return 0;
  if(prime[n-1]){
    cout<<0<<"\n";
    continue;
  }
  int ind=lower_bound(all(prim),n)-prim.begin();
  cout<<prim[ind]-prim[ind-1]<<"\n";
}
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <utility>
#include <cstring>
using namespace std;

#define rep(i,n) for(int i=0; i<n; i++)
typedef long long ll;

const int MAX = 1300000;
bool prime[MAX];

int main() {
	prime[0] = prime[1] = true;
	for(int i=2; i*i<MAX; i++) {
		if(!prime[i]) {
			for(int j=i+i; j<MAX; j+=i) {
				prime[j] = true;
			}
		}
	}
	vector<int> primes;
	for(int i=2; i<MAX; i++) {
		if(!prime[i])
			primes.push_back(i);
	}

	int n;
	while(cin >> n, n) {
		int l = 0;
		while(primes[l+1]<n) l++;
		if(n==2 || n == primes[l+1]) cout << 0 << endl;
		else cout << primes[l+1]-primes[l] << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
#define MAX 2000000
bool isP[MAX];
int ans[MAX];

void make_table(){
  for(int i=0;i<MAX;i++)isP[i]=true;
  isP[0]=isP[1]=false;
  for(int i=2;i*i<MAX;i++){
    if(!isP[i])continue;
    for(int j=i*i;j<MAX;j+=i)isP[j]=false;
  }

  for(int i=1;i<MAX;i++){
    if(isP[i])continue;
    ans[i]=ans[i-1]+1;
  }
  for(int i=MAX-2;i>=0;i--){
    if(isP[i])continue;
    ans[i]=max(ans[i],ans[i+1]);
  }
}

int n;
int main(){
  make_table();
  while(1){
    cin>>n;
    if(n==0)break;
    cout<<(ans[n]>0?ans[n]+1:0)<<endl;
  }
  return 0;
}