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

int pmem[130000];
vector<int> prime;
void init()
{
  for(int i = 2; i < 1300000; i++)
  {
    if(pmem[i])
      continue;
    pmem[i] = 1;
    prime.push_back(i);
    for(int j = i + i; j < 1300000; j += i)
      pmem[j] = -1;
  }
}

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

C++: #include <cstdio>
#include <cctype>
#include <utility>
#include <cmath>
#include <algorithm>
using namespace std;

const auto gcu = getchar_unlocked;
const auto pcu = putchar_unlocked;
#define _T template <typename T>
#define _HT template <typename H, typename... T>
#define _il inline
#define _sc _il bool scan
_T T in(int c){T n=0;int m=1;while(isspace(c)){c=gcu();}if(c=='-')m=-1,c=gcu();
	do{n=10*n+(c-'0'),c=gcu();}while(c>='0'&&c<='9');return m*n;}
_il int in() {return in<int>(gcu());}
_T T scan(T &n){int c=gcu();return c==EOF?false:(n=in<T>(c),true);}
_sc(char &c){c=gcu();gcu();return c!=EOF;}
#ifdef _GLIBCXX_STRING
_sc(string &s){int c;s="";
	for(;;){c=gcu();if(c=='\n'||c==' ')return true;else if(c==EOF)return false;s+=c;}}
#endif
_HT _sc(H &h, T&&... t){return scan(h)&&scan(t...);}
#define _ot _il void out
#define _ol _il void outl
_ot(bool b) {pcu('0'+b);}
_ot(const char *s){while(*s)pcu(*s++);}
_ot(char c){pcu(c);}
#ifdef _GLIBCXX_STRING
_ot(string s){for(char c:s)pcu(c);}
#endif
_T _ot(T n){static char b[20];char *p=b,m=n<0?pcu('-'),-1:1;
	if(!n)*p++='0';else while(n)*p++=n%10*m+'0',n/=10;while(p!=b)pcu(*--p);}
_ol(){out('\n');}
#ifdef _GLIBCXX_VECTOR
_T _ot(vector<T> &v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);outl();}
#endif
_HT _ot(H&& h, T&&... t){out(h);out(move(t)...);}
template <typename... T> _ol(T&&... t){out(move(t)...);outl();}

template<int N>
struct sieve {
	bool s[N];
	int v[N], l = -1;
	sieve() : s(), v() {
		for (int i = 0; i < N; i++)
			s[i] = true;
		for (int i = 4; i < N; i += 2)
			s[i] = false;
		for (int i = 9; i < N; i += 6)
			s[i] = false;
		for (int i = 6, e = sqrt(N); i <= e; i += 6) {
			for (int j : {i - 1, i + 1})
				if (s[j])
					for (int k = j * j; k < N; k += j)
						s[k] = false;
		}
		for (int i = 2; i < N; i++)
			if (s[i])
				v[++l] = i;;
	}
};

int main() {
	sieve<1300000> s;
	for (int n; (n = in());) {
		if (s.s[n])
			outl(0);
		else {
			auto i = lower_bound(s.v, s.v + s.l, n);
			outl(*i - *(i - 1));
		}
	}
}

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<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

const int LIM=1300000;
bool sosu[LIM];

int main() {
	int i,j;
	for(i=0;i<LIM;i++)sosu[i]=true;
	sosu[0]=sosu[1]=false;
	for(i=0;i<LIM;i++){
		if(sosu[i]){
			for(j=i*2;j<LIM;j+=i){
				sosu[j]=false;
			}
		}
	}
	int n;
	while(n=in()){
		if(sosu[n]){
			cout<<0<<endl;
			continue;
		}
		int i,j;
		for(i=n;!sosu[i];i--);
		for(j=n;!sosu[j];j++);
		cout<<j-i<<endl;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
int primes[100003];
bool is(int n){
	for(int i=2;i*i<=n;i++)if(n%i==0)return false;
	return true;
}
int main(){
	int cnt=0,ii=2;
	while(true){
	if(cnt==100003)break;
	if(is(ii)){
	primes[cnt]=ii;
	cnt++;
	}
	ii++;
	}
	int n;
	while(cin>>n){
	int a=0,b=0;
	bool f=0;
	for(int i=0;i<100003;i++){
	if(primes[i]<=n)a=primes[i];
	if(f==0&&primes[i]>=n)b=primes[i],f=1;
	}
	if(n==1)cout<<0<<endl;
	else cout<<b-a<<endl;
	}
	return 0;
}

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

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

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