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 <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


namespace{
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}


#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;

bool is_prime(int a){
	for(int i=2;i*i<=a;i++){
		if(a%i==0) return false;
	}
	return true;
}
void mainmain(){
	
	int n;
	while(cin>>n,n){
		if(is_prime(n)){
			cout<<0<<endl;
			continue;
		}
		int x=n-1,y=n+1;
		while(!is_prime(x--));
		while(!is_prime(y++));
		cout<<y-x-2<<endl;
	}
}



}
main() try{
    mainmain();
}
catch(...){
}

C++: #include <cstdio>
#include <vector>
#include <algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
const int c=1300000;
bool prime[c+1];
vector<int> pr;
void makeprime(){
	rep(i,c+1) prime[i]=1;
	prime[0]=0,prime[1]=0;
	for(int i=2;i+i<=c;i++) if(prime[i]) for(int j=2;j*i<=c;j++) prime[i*j]=0;
	rep(i,c+1) if(prime[i]) pr.push_back(i);
}
int main(){
	makeprime();
	while(true){
		int n;
		scanf("%d",&n);
		if(!n) break;
		if(prime[n]){
			printf("0\n");
			continue;
		}
		printf("%d\n",pr[upper_bound(pr.begin(),pr.end(),n)-pr.begin()]-pr[upper_bound(pr.begin(),pr.end(),n)-pr.begin()-1]);
	}
}

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

using namespace std;

bool is_prime[(1 << 21) + 10];
vector<int> prime;

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

int main(void) {
  sieve(1 << 21);
  
  int n;
  while (cin >> n, n) {
    auto p = upper_bound(prime.begin(), prime.end(), n);
    cout << (is_prime[n] ? 0 : *p - *(p - 1)) << endl;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repp(i, n) rep(i, 0, n)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;

const int MAX_N = 2000000;
vector<int> prime;
bool is_prime[MAX_N + 1];
void sieve(int n){
    rep(i, 0, n + 1) is_prime[i] = true;
    is_prime[0] = is_prime[1] = false;
    rep(i, 2, n + 1){
        if(is_prime[i]){
            prime.push_back(i);
            for(int j = 2*i; j <= n; j += i) is_prime[j] = false;
        }
    }
}

signed main(){
    sieve(MAX_N);
    int n;
    while(cin >> n, n){
        if(is_prime[n]){
            cout << 0 << endl;
            continue;
        }
        int ind = upper_bound(all(prime), n) - prime.begin();
        cout << prime[ind] - prime[ind - 1] << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int (i)=(0);(i)<(int)(n);++(i))
using ll = long long;

const int MAX_N = 1300000;

int prime[MAX_N];
bool is_prime[MAX_N + 1];

void eratos(int n) {
    int p = 0;
    for (int i = 0; i <= n; ++i) is_prime[i] = true;
    is_prime[0] = is_prime[1] = false;
    for (int i = 2; i <= n; ++i) {
        if (is_prime[i]) {
            prime[p++] = i;
            for (int j = 2*i; j <= n; j += i) is_prime[j] = false;
        }
    }
}

int main() {
    eratos(MAX_N);

    int n;
    while (cin >> n, n) {

        int r = n, l = n;
        for (int i=n; !is_prime[i]; --i) l=i-1;
        for (int i=n; !is_prime[i]; ++i) r=i+1;

        cout << r-l << endl;

    }
}

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

bool isprime(int a) {
	for (int i = 2;i*i <= a;i++) {
		if (a%i == 0)return false;
	}
	return a != 1;
}

int main() {
	int n;
	while (cin >> n, n) {
		int ans;
		int sp, bp;
		if (isprime(n))ans = 0;
		else {
			for (int i = n;i > 0;i--) {
				if (isprime(i)) {
					sp = i;
					break;
				}
			}
			for (int i = n;i < 1300000;i++) {
				if (isprime(i)) {
					bp = i;
					break;
				}
			}
			ans = bp - sp;
		}
		cout << ans << endl;
	}
	return 0;
}