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>
#define M 1299709
using namespace std;
void make(vector<bool>& a){
a[0]=false;
a[1]=false;
for(int i=2;i<a.size();i++){
	if(a[i]){
		for(int j=2;j*i<a.size();j++){
			a[i*j]=false;
		}
	}

}
return;
	}

int main(void){
	vector<bool> a(M,true);
	make(a);
	
	while(1){
		int n;
		cin>>n;
		if(n==0)break;
		int up=0,down=0;
		while(1){
			if(a[n+up]){
				break;
			}else{
				up++;
			}
		}
		while(1){
			if(a[n-down]){
				break;
			}else{
				down++;
			}
		}
		cout<<up+down<<endl;
	}
	return 0;
}

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

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
//#define LOG(...)
#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define RREP(i, n) for(int i = (int)(n - 1); i >= 0; --i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SQ(n) (n) * (n)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vc> vvc;
typedef vector<vll> vvll;

const int N = 1299709;   // main??????????§??????¨?\¨
bool sieve[N + 1] = {1, 1, 0};   // false:?´???°

int main() {
  vi primes;

  FOR(i, 2, N + 1){
    if (sieve[i]) continue;
    primes.push_back(i);
    for(int j = i * 2; j < N  + 1; j += i) {
      sieve[j] = true;
    }
  }

  int n;
  while(cin >> n, n) {
    if(!sieve[n]) cout << 0 << endl;
    else{
      auto sprime = lower_bound(ALL(primes), n);
      cout << *sprime - *(sprime - 1) << endl;
    }
  }
}

C++: #include <iostream>
#define N 1200000
using namespace std;
int start(int);
int goal(int);
int sosu[N];
int main(){
  int i,j,n,s,g;
  for(i=0;i<N;i++) sosu[i]=1;
  sosu[0]=0;
  sosu[1]=0;
  for(i=0;i<N/2;i++){
    if(sosu[i]==1){
      for(j=i*2;j<N;j+=i){
	sosu[j]=0;
      }
    }
  }
  while(1){
    cin >> n;
    if(n==0) break;
    s=start(n);
    g=goal(n);
    if(s==0||g==0) cout << 0 << endl;
    else cout << g-s << endl;
  }
  return 0;
}
int start(int n){
  int i,flag=0,result;
  for(i=n;i>=2;i--){
    if(sosu[i]==1){
      if(i==n){
	break;
      }
      flag=1;
      result=i;
      break;
    }
  }
  if(flag==1) return result;
  else return 0;
}
int goal(int n){
  int i,flag=0,result;
  for(i=n;i<N;i++){
    if(sosu[i]==1){
      if(i==n){
	break;
      }
      flag=1;
      result=i;
      break;
    }
  }
  if(flag==1) return result;
  else return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; }
#endif

#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CLEAR(arr, val) memset(arr, val, sizeof(arr))

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

template <class T> void max_swap(T& a, const T& b) { a = max(a, b); }
template <class T> void min_swap(T& a, const T& b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double PI = acos(-1.0);
const int dx[] = { 0, 1, 0, -1 };
const int dy[] = { 1, 0, -1, 0 };



const int MAX_PRIME = 1299709;
bool is_prime[MAX_PRIME + 1];
void sieve(int n = MAX_PRIME)
{
	fill_n(is_prime, n + 1, true);
	is_prime[0] = is_prime[1] = false;
	for (int i = 2; i * i <= n; ++i)
	{
		if (is_prime[i])
		{
			for (int j = 2 * i; j <= n; j += i)
				is_prime[j] = false;
		}
	}
}
vector<int> make_primes(int n = MAX_PRIME)
{
	vector<int> res;
	for (int i = 2; i <= n; ++i)
		if (is_prime[i])
			res.push_back(i);
	return res;
}
int main()
{
	sieve();
	vector<int> p = make_primes();
	int n;
	while (scanf("%d", &n), n)
	{
		if (is_prime[n])
			puts("0");
		else
		{
			int t = lower_bound(all(p), n) - p.begin();
			printf("%d\n", p[t] - p[t - 1]);
		}
	}
}

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

using namespace std;

#define MAX 1299710
#define SQRTMAX 1141

bool prime[1299709];

int main()
{
    prime[0] = prime[1] = false;
    fill(prime+2, prime+MAX, true);
    for(int p=2; p<SQRTMAX; p++)
    {
        if(prime[p])
        {
            for(int i=2*p; i<MAX; i+=p)
            {
                prime[i] = false;
            }
        }
    }

    int n;
    while(cin >> n, n)
    {
        if(prime[n])
        {
            cout << 0 << endl;
        }
        else
        {
            int f = n;
            int t = n;
            while(!prime[f]) f--;
            while(!prime[t]) t++;
            cout << t - f << endl;
        }

    }
}