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

bool isPrime(int x) { for(int i=2; i*i<=x; i++) if(x%i==0) return false; return true; }

int main() {
  
  int n;
  while(cin >> n, n) {
    int ans = 0;
    for(int i=0; !isPrime(n+i); i++, ans++);
    for(int i=0; !isPrime(n-i); i++, ans++);
    cout << ans << endl;
  }
  
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <string.h>

using namespace std;
bool IsPrime[1300000];
vector<int> Prime;

void calc()
{
	memset(IsPrime, 1, sizeof(bool) * 1300000);
	for(int i = 2; i < 1300000; ++i)
	{
		if(IsPrime[i])
		{
			Prime.push_back(i);
			for(int j = 2 * i; j < 1300000; j += i)
			{
				IsPrime[j] = false;
			}
		}
	}
}

void solve()
{
	calc();
	int n;
	while(cin >> n, n)
	{
		if(IsPrime[n])
		{
			cout << 0 << endl;
		}
		else
		{
			int pos = 0;
			while(Prime[pos] < n)
			{
				pos++;
			}
			cout << Prime[pos] - Prime[pos - 1] << endl;
		}
	}
}

int main()
{
	solve();
	return(0);
}

C++: #include <bits/stdc++.h>
using namespace std;

const int MAXN = 1300000;
int isP[MAXN];
vector<int> Prime, rPrime;

int main() {
  memset(isP, -1, sizeof(isP));
  isP[0] = isP[1] = false;
  for(int i = 2; i < MAXN; ++i) {
    if(!isP[i]) continue;
    Prime.push_back(i);
    for(int j = i*2; j < MAXN; j += i) isP[j] = false;
  }
  rPrime = Prime;
  reverse(rPrime.begin(), rPrime.end());
  for(int n; cin >> n && n;) {
    int a = *lower_bound(rPrime.begin(), rPrime.end(), n, greater<int>());
    int b = *lower_bound(Prime.begin(), Prime.end(), n);
    if(a == n || b == n) cout << 0 << endl;
    else cout << b - a << endl;
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
#include<cmath>
bool isprime(int n) {
	if (n == 2)return true;
	if (n % 2 == 0)return false;
	for (int i = 3; i <= sqrt(n); i += 2) if (n%i == 0)return false;
	return true;
}
int main() {
	int n;
	while (cin >> n, n) {
		if (isprime(n))cout << "0\n";
		else {
			int r, l;
			for (r = n - 1; !isprime(r); r--);
			for (l = n + 1; !isprime(l); l++);
			cout << l - r << endl;
		}
	}
}

C++: #include <cstdio>
#define MAX (2000000)
using namespace std;

bool prime[MAX + 1];

int main()
{
	for (int i = 2; i <= MAX; i++){
		prime[i] = true;
	}
	
	for (int i = 2; i * i <= MAX; i++){
		for (int j = i * i; j <= MAX; j += i){
			prime[j] = false;
		}
	}
	
	int n;
	while (scanf("%d", &n), n){
		bool *p;
		int cnt = 0;
		
		p = &prime[n];
		while (*p == false){
			cnt++;
			p--;
		}
		
		p = &prime[n];
		while (*p == false){
			cnt++;
			p++;
		}
		
		printf("%d\n", cnt);
	}
	
	return 0;
}

C++: #include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <complex>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <bitset>
#include <ctime>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cassert>
#include <cstddef>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <sstream>
#include <fstream>

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 RREP(i, a) for(int (i) = (a) - 1; (i) >= 0; (i)--)
#define FORR(i, a, b) for(int (i) = (a) - 1; (i) >= (b); (i)--)
#define DEBUG(C) cerr << #C << " = " << C << endl;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) rbegin((a)), rend((a))
#define SORT(a) sort(ALL((a)))
#define RSORT(a) sort(RALL((a)))
#define REVERSE(a) reverse(ALL((a)))
#define MP make_pair
#define FORE(a, b) for (auto &&a : (b))
#define FIND(s, e) ((s).find(e) != (s).end())
#define EB emplace_back

const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;

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

void solve(int k) {
    if (k == 1 || isPrime(k)) {
        cout << 0 << endl;
        return;
    }
    int ans = 0;
    for (int i = k - 1; !isPrime(i); i--) {
        ans++;
    }
    for (int i = k; !isPrime(i); i++) {
        ans++;
    }
    cout << ans + 1 << endl;
    return;
}

signed main(void) {
    int n, m, p, a, b, c, x, y, z, q;
    string s;
    bool f = false;
    while (cin >> n, n) {
        solve(n);
        //return 0;
    }
}

C++: #include <bits/stdc++.h>
#define MOD 1000000007
typedef long long ll;
using namespace std;

int N;

void solve(){
  bool prime=true;
  for(ll i=2;i*i<=N;i++){
    if(N%i==0){
      prime=false;
      break;
    }
  }
  if(prime){
    cout<<0<<endl;
    return;
  }

  int ans=2;
  for(int k=N-1;k>1;k--){
    bool prime=true;
    for(ll i=2;i*i<=k;i++){
      if(k%i==0){
        prime=false;
        break;
      }
    }
    if(prime) break;
    ans++;
  }
  for(int k=N+1;;k++){
    bool prime=true;
    for(ll i=2;i*i<=k;i++){
      if(k%i==0){
        prime=false;
        break;
      }
    }
    if(prime) break;
    ans++;
  }

  cout<<ans<<endl;
}

int main(){
  while(1){
    cin>>N;
    if(N==0) break;
    solve();
  }

  return 0;
}