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++: #define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<utility>
#include<iomanip>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<list>
#include<set>
#include<stdlib.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define repp(i,k,n) for(int i=k;i<(int)n;i++)
using namespace std;
const int dx[4] = { 0, 1, 0, -1 }, dy[4] = { 1, 0, -1, 0 };
const int dX[8] = { 0, 1, 1, 1, 0, -1, -1, -1 }, dY[8] = { 1, 1, 0, -1, -1, -1, 0, 1 };
vector<long long> Prime;
void SieveOfEratosthenes(long long e) //setup vector<long long>Prime(~x)
{
	bool *x=new bool[e];
	for (long long i = 0; i <= e; i++)x[i] = false;
	for (long long i = 2; i <= e; i++)
	{
		if (x[i]);
		else
		{
			Prime.push_back(i);
			for (long long l = i; l <= e; l += i)x[l] = true;
		}
	}
	return;
}
struct XY
{
	int x;
	int y;
};
int in()
{
	int x;
	cin >> x;
	return x;
}
/*
(char)'1' - (int)48 = (int)1;
???1111181
*/
////////////////////////////////////////////////////



int main()
{
	SieveOfEratosthenes(10000000);
	int n;
	while (cin >> n&&n)
	{
		if (binary_search(Prime.begin(), Prime.end(), n))
		{
			cout << 0 << endl;
		}
		else
		{
			cout << *lower_bound(Prime.begin(), Prime.end(), n) - *(lower_bound(Prime.begin(), Prime.end(), n) - 1) << endl;
		}


	}
	
}

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

using namespace std;

int k;
vector<int> pvec;
bool prime[1500005];

int main(void)
{
  for(int i = 2; i < 50005; i++){
    if(prime[i]) continue;
    for(int j = 2*i; j < 1500005; j+=i) prime[j] = true;
  }
  for(int i = 2; i < 1500005; i++){
    if(!prime[i]) pvec.push_back(i);
  }

  while(1){
    cin >> k;
    if(k == 0) break;
    int u = *lower_bound(pvec.begin(), pvec.end(), k);
    if(u == k){
      cout << 0 << endl;
      continue;
    }
    int l = *(lower_bound(pvec.begin(), pvec.end(), k) - 1);
    cout << u - l << endl;
  }
  return 0;
}

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

using namespace std;

bool p[2000001];
int pr[2000001];

int main()
{
    p[0] = p[1] = 1;
    for (int i = 4; i <= 2000000; i += 2) p[i] = 1;
    
    
    int ind = 0;
    pr[ind++] = 2;
    for (int i = 3; i * i <= 2000000; i += 2){
        if (!p[i]){
            for (int j = 3 * i; j <= 2000000; j += 2 * i) p[j] = 1;
        }
    }
    for (int i = 3; i <= 2000000; i++) if (!p[i]) pr[ind++] = i;
    
    int n;
    while (scanf("%d", &n) && n){
        if (binary_search(pr, pr + 100000, n) == 1) printf("0\n");
        else printf("%d\n", *upper_bound(pr, pr + 100000, n) - *(lower_bound(pr, pr + 100000, n) - 1));
    }
    
    return (0);
}

C++: #include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
 
#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define pi M_PI
 
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

const int N = 1300000;

int main() {
    VI primes, d(N,1);
    FOR(i,2,N-1){
        if (!d[i]) continue;
        primes.push_back(i);
        int x = 2*i;
        while (x < N){
            d[x] = 0;
            x += i;
        }
    }
    int n;
    while (cin >> n && n){
        int i = 0;
        while (primes[i] < n) i++;
        cout << (primes[i] == n ? 0 : primes[i]-primes[i-1]) << endl;
    }

    return 0;
}

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

using namespace std;

const int N = 1299709;

vector<int> prime;
bool is_prime[N + 1];

void sieve(){
  fill(is_prime, is_prime+N+1, true);
  is_prime[0] = is_prime[1] = false;
  for(int i=2;i<=N;i++){
    if(is_prime[i]){
      prime.push_back(i);
      for(int j=2*i;j<=N;j+=i) is_prime[j] = false;
    }
  }
}

int solve(int n){
  if(is_prime[n]) return 0;
  vector<int>::iterator it = upper_bound(prime.begin(), prime.end(), n);
  int r = *it;
  *it--;
  int l = *it;
  return r-l;
}

main(){
  int n;
  sieve();
  while(cin >> n && n){
    cout << solve(n) << endl;
  }
}

C++: #include <cstdio>
#include <iostream>
#include <cmath>
#include <ctype.h>
#include <string> 
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

bool isprime(int n);

int main()
{
	int k;
	while(1){
		cin >> k;
		if(k == 0) break;
		int ans;
		if(isprime(k)) ans = 0;
		else {
			int i = 1, j = 1;
			while(1){
				if(isprime(k - i)) break;
				else i++;
			}
			while(1){
				if(isprime(k + j)) break;
				else j++;
			}
			ans = i + j;
		}
		cout << ans << endl;
	}
	return 0;
}

bool isprime(int n)
{
	if(n == 1 || n == 0) return false;
	else if(n == 2) return true;
	else {
		for(int i = 2; i * i <= n; i++){
			if(n % i == 0) return false; 
		}
	}
	return true;
}