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

vector<int> createPrime ( int num )
{
	vector<int> vp;
	vector<bool> vb(num+1, true);
	vb[0] = vb[1] = false;
	for (int i = 2; i <= num; ++i) {
		if (!vb[i]) { continue; }
		vp.push_back(i);
		for (int j = i+i; j <= num; j+=i) { vb[j] = false; }
	}
	return vp;
}

const int MAX = 1299709;

int main() {
	int k;
	vector<int> vp = createPrime(MAX);

	while (cin >> k, k) {
		vector<int>::iterator it = lower_bound(vp.begin(), vp.end(), k);
		if (*it == k || k == 1) {
			cout << 0 << endl;
		}
		else {
			vector<int>::iterator it2 = it - 1;
			cout << *it - *it2 << endl;
		}
	}
	return 0;
}

C++: #include <iostream>

using namespace std;

#define N 1299710

int main(int argc, char const *argv[])
{
	int n;
	int p1,p2;

	bool f[N];

	f[0]=true;
	f[1]=true;
	f[2]=false;
	for(int i1=0;i1<N;i1++){
		if(f[i1]==false){
			for(int i2=2;i2*i1<N;i2++){
				f[i2*i1]=true;
			}
		}
	}

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

		p1=n;
		while(f[p1]==true) p1--;
		p2=n;
		while(f[p2]==true) p2++;

		cout<<p2-p1<<endl;

	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
bool is_prime(int n){
  for (int i = 2; i * i <= n; i++){
    if (n % i == 0){
      return false;
    }
  }
  return true;
}
int main(){
  while (1){
    int n;
    cin >> n;
    if (n == 0){
      break;
    }
    if (is_prime(n)){
      cout << 0 << endl;
    } else {
      int L = n;
      while (!is_prime(L)){
        L--;
      }
      int R = n;
      while (!is_prime(R)){
        R++;
      }
      cout << R - L << endl;
    }
  }
}

C++: #include <vector>
#include <iostream>
#include <utility>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <tuple>
#include <queue>
#include <functional>
#include <cmath>
#include <iomanip>
#include <map>
#include <set>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <complex>
#include <iterator>
#include <array>
#include <memory>
#include <stack>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define all(in) in.begin(), in.end()
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
#define MAX 1299710
using namespace std;
typedef pair<int, int> pii;
typedef pair<double,double>pdd;
typedef pair<ll,ll>pll;
int main(){
    vector<bool>IsPrime(MAX+1,true);
    vector<int>Prime(MAX+1,0); //Primearray
    int Primecounter =0;                  //Primearray counter;
    for(int i = 2; i<MAX+1;i++){
        if(IsPrime[i]){
            for(int j = 2;i*j<MAX;j++)
                IsPrime[i*j] = false;//?´???°??????Primearray???????´??????????????????°?????¨???false???
            Prime[Primecounter] = i;
            Primecounter++;
        }
    }
    ll hoge;
    while(cin>>hoge,hoge){
    ll a,b;
    for(ll i=hoge; ;i++){
        if(IsPrime[i]){
            a=i; break;
        }
        
    }
    for(ll j=hoge;; j--){
        if(IsPrime[j]){
            b=j; break;
        }
    }
    cout<<a-b<<endl;
    }
}

C++: #include<iostream>
#include<cmath>
#include<vector>
#define N 1299709

using namespace std;

vector<int> MakePrimeNumber(void);

int main(){
  int i, n, x;
  vector<int> data;

  data = MakePrimeNumber();

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

    for(i=0; i<data.size(); ++i){
      if(n == data[i]){
	x = 0;
	break;
      }else if(n < data[i]){
	x = data[i] - data[i-1];
	break;
      }
    }
    cout << x << endl;
  }
  return 0;
}

vector<int> MakePrimeNumber(void){
  int i, j, k;
  vector<int> data;

  data.push_back(2);
  data.push_back(3);
  for(i=5; i<=N; i+=2){
    for(j=1, k=0; data[j]<=sqrt(i); ++j){
      if(i%data[j] == 0){
	k = 1;
	break;
      }
    }
    if(k == 0) data.push_back(i);
  }
  return data;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <cmath>
#include <cstdio>
#include <istream>
#include <sstream>
#include <iomanip>
using namespace std;

typedef ostringstream oss;
typedef istringstream iss;

vector<bool> Prime;
vector<int> PrimeStk;
void initPrime(long num) {
	Prime = vector<bool>(num + 1, true);
	Prime[1] = Prime[0] = false;
	long sq_num = sqrt((double)num);
	for (long i = 2; i < sq_num; i++) {
		if (Prime[i]) {
			for (long j = i + i; j <= num; j += i) {
				Prime[j] = false;
			}
		}
	}
}
void initPrimeStk(long begin) {
	PrimeStk.clear();
	for (long i = begin; i < Prime.size(); i++) {
		if (Prime[i]) {
			PrimeStk.push_back(i);
		}
	}
}

int main(void) {
	initPrime(1299709);
	initPrimeStk(0);

	int N;
	while (cin >> N, N) {
		if (Prime[N]) {
			cout << 0 << endl;
			continue;
		}
		vector<int>::iterator it = upper_bound(PrimeStk.begin(), PrimeStk.end(), N);

		cout << *it - *(it - 1) << endl;
	}


	return 0;
}