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 <algorithm>
#include <vector>
using namespace std;

const int C = 1300000;
bool isprime[C];
vector<int> prime;

void init() {
  fill(isprime, isprime+C, true);
  isprime[0] = isprime[1] = false;
  for (auto i=2; i<C; i++) {
    if (isprime[i]) {
      prime.push_back(i);
      for (auto j=2; j<C; j++) {
	if ( i * j >= C) break;
	isprime[i*j] = false;
      }
    }
  }
}

int main() {
  init();
  int N;
  while (cin >> N && N) {
    if (isprime[N]) {
      cout << "0" << endl;
    } else {
      int p = 0;
      int q = 0;
      for (auto i=0; i<prime.size(); i++) {
	if (prime[i] < N) p = prime[i];
	else if (prime[i] > N) {
	  q = prime[i];
	  break;
	}
      }
      cout << q-p << endl;
    }
  }
}

C++: #include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
bool f[1300000];
int main(){
    fill(f,f+1300000,true);
    f[0]=f[1]=false;
    vector<int>prime(1,1);
    for(int i=2;i<=1300000;i++){
        if(!f[i])continue;
        prime.push_back(i);
        for(int j=i+i;j<=1300000;j+=i)f[j]=false;
    }

    int n;
    while(scanf("%d",&n),n){
        int pos=lower_bound(prime.begin(),prime.end(),n)-prime.begin();
        if(prime[pos]==n){
            puts("0");
        }
        else{
            printf("%d\n",prime[pos]-prime[pos-1]);
        }
    }

    return 0;
}

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

#define REP(i,a,b) for(i=a; i<b; ++i)
#define rep(i,n) REP(i,0,n)

int isprime[1299710];

int main() {
  int i,j,k;
  memset(isprime, 0, sizeof(isprime));
  for(i=4; i<1299710; i+=2) isprime[i] = 1;

  for(i=3; i<=1141; i+=2)
    if(isprime[i] == 0)
      for(j=2; i*j<1299710; ++j)
	isprime[i*j] = 1;

  int in;
  while(cin>>in, in) {
    if(isprime[in] == 0) {
      cout<<0<<endl;
      continue;
    }

    int up=0,low=0;
    while(isprime[in+up] == 1) up++;
    while(isprime[in-low] == 1) {
      low++;
    }
    cout<<up+low<<endl;
  }
}

C++: #include <stdio.h>
#include <math.h>
#define MAXNUM 1299709

int main(void){
	int flag[MAXNUM + 1], i, j, n;
	for(i = 0;i <= MAXNUM;i++)
		flag[i] = 1;
	for(i = 2;i <= sqrt((double)MAXNUM);i++){
		for(j = i * 2;j <= MAXNUM;j += i)
			flag[j] = 0;
	}
	flag[0] = flag[1] = 0;
	while(1){
		scanf("%d",&n);
		if(!n)
			break;
		for(i = n;!flag[i];i++);
		for(j = n;!flag[j];j--);
		printf("%d\n",j - i);
	}
	return 0;
}

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

using namespace std;

#define all(c) (c).begin(), (c).end()
const int N = 1299709;

template<typename T> inline T sqr(T x){return x * x;}

void sieve(vector<int>& v, int n){
    v.assign(n, 0); iota(all(v), 2);
    for(auto div=v.begin(); sqr(*div)<=v.back(); ++div){
        v.erase(remove_if(div+1, v.end(), [&](int x){return x % *div == 0;}), v.end());
    }
}

int main(){
    vector<int> primes;
    sieve(primes, N);
    for(int k; cin >> k, k;){
        auto itr = lower_bound(all(primes), k);
        cout << (*itr == k? 0: *itr - *(itr - 1)) << '\n';
    }
    return 0;
}

C++: #include <cstdio>
#include <utility>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#define REP(i,n) for(int i=0;i<n;i++)
typedef long long ll;
using namespace std;
typedef vector<ll> vl;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vs;

#define M 1299709 

int main() 
{
	bool t[M+1]={}; //素数ならfalse
	t[0] = true;
	t[1] = true;
	
	for(int i=2;i*i<=M;i++)
	{
		if(!t[i])
		{
			for(int j = i*2 ; j<=M ; j += i)
			{
				t[j] = true;
			}
		}
	}
	
	ll x;
	while(cin >> x && x!=0)
	{
		ll min(0),max(0);
		if(!t[x]){cout << 0 << endl;}
		else{
			for(int j=0;;j++){if(!t[x+j+1]){max=x+j+1;break;}}
			for(int j=0;;j--){if(!t[x+j-1]){min=x+j-1;break;}}
			cout << max - min << endl;
		}
	}
	return 0 ;
	
}

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

int prime[1300000];
void init()
{
  for(int i = 2; i < 1300000; i++)
    prime[i] = -1;
  for(int i = 2; i < 1300000; i++)
  {
    if(prime[i] != -1)
      continue;
    prime[i] = 1;
    for(int j = i + i; j < 1300000; j += i)
      prime[j] = 0;
  }
}

int main()
{
loop:
  init();
  int n;
  cin >> n;
  if(!n)
    return 0;
  if(prime[n])
  {
    cout << 0 << endl;
    goto loop;
  }
  int b;
  for(int i = 0; i < 1300000; i++)
  {
    if(prime[i] && i > n)
    {
      cout << i - b << endl;
      goto loop;
    }
    else if(prime[i])
      b = i;
  }
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
bool sosu[1299710];
void SOSU(){
  memset(sosu,false,sizeof(sosu));
  for(int i=2;i<1299710;i++){
    if(sosu[i]==false){
      for(int j=2;i*j<1299710;j++){
	sosu[i*j]=true;
      }
    }
  }
}
int main(){
  int n,sum,lI,lII;
  SOSU();
  while(1){
    sum=0;
    cin >> n;
    if(n == 0) break;
    for(lI=n;sosu[lI]==true && lI>=2;lI--);
    for(lII=n;sosu[lII]==true;lII++);
    cout << lII-lI << endl;
  }
}