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 <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str();}
typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EACH(t,i,c) for(t::iretator i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int MAX = 1299709;

int main() {
	vi prime(MAX+1, 1);
	FOR(i, 2, MAX) {
		if(prime[i]) {
			for(int j = 2*i; j <= MAX; j+=i) {
				prime[j] = 0;
			}
		}
	}

	int n;
	while(cin >> n, n) {
		int ans = 0;
		int left, right;
		left = right = n;
		while(!prime[left]) {
			ans++;
			left--;
		}

		while(!prime[right]) {
			ans++;
			right++;
		}

		cout << ans << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<set>
using namespace std;
typedef long long int integer;

const int NMAX = 1299800;
bool isprime[NMAX];

int main() {
  vector<integer> primes;
  set<int> prime_set;
  for (int i = 0; i < NMAX; i++) {
    isprime[i] = true;
  }
  isprime[0] = isprime[1] = false;
  for (integer n = 2; n < NMAX; n++) {
    if (!isprime[n]) continue;
    primes.push_back(n);
    prime_set.insert(n);
    integer m = 2 * n;
    while (m < NMAX) {
      isprime[m] = false;
      m += n;
    }
  }

  int n;
  while (cin >> n && n) {
    if (n <= 2 or prime_set.find(n) != prime_set.end()) {
      cout << 0 << endl;
    } else {
      auto upper = upper_bound(primes.begin(), primes.end(), n);
      auto lower = lower_bound(primes.begin(), primes.end(), n) - 1;
      cout << (*upper) - (*lower) << endl;
    }
  }
}

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

#define fi first
#define se second
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define each(itr,v) for(auto itr:v)
#define pb(s) push_back(s)
#define mp(a,b) make_pair(a,b)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define maxch(x,y) x=max(x,y)
#define minch(x,y) x=min(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;

#define INF INT_MAX/3

#define MAX_N 1000

int n;
bool is_prime[1300000];

int main(){
	cin.sync_with_stdio(false);
	rep(i,1300000)is_prime[i]=true;
	is_prime[0]=is_prime[1]=false;
	repl(i,2,1300000){
		if(is_prime[i]){
			for(int j=i*2;j<1300000;j+=i)is_prime[j]=false;
		}
	}
	while(1){
		cin>>n;
		if(n==0)break;
		if(is_prime[n])cout<<0<<endl;
		else{
			int l=n,r=n;
			while(!is_prime[l])l--;
			while(!is_prime[r])r++;
			cout<<r-l<<endl;
		}
	}
	return 0;
}

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

int pri(int j){
  if(j==1)return 0;
  else if(j==2)return 1;
  else if(j%2==0)return 0;
  for(int i=3;i*i<=j;i+=2){
    if(j%i==0)return 0;
  }
  return 1;
}

int main(){
  int n,sum;
  while(1){
    cin>>n;
    if(n==0)break;
    sum=0;
    for(int i=n;;i++){
      if(pri(i)==1)break;
      else sum++;
    }
    for(int i=n;i>=1;i--){
      if(pri(i)==1)break;
      else sum++;
    }
    cout<<sum<<endl;
  }
  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 <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 114514810
#define ALL(a) (a).begin(),(a).end()
#define pb(x) push_back(x)
#define ms(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(10) 
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge{ int from, to, cost; };

bool prime[2000000];
void hurui()
{
	memset(prime, true, sizeof(prime));
	prime[0] = prime[1] = false;

	for (int i = 2; i*i < 2000000; i++)
	{
		for (int j = 2; i*j < 2000000; j++)
		{
			prime[i*j] = false;
		}
	}
}

int main()
{
	hurui();
	int n;
	while (cin >> n, n)
	{
		int u, d;
		for (int i = n; i > 0; i--)
		{
			if (prime[i] == true)
			{
				d = i; break;
			}
		}
		for (int i = n; i > 0; i++)
		{
			if (prime[i] == true)
			{
				u = i; break;
			}
		}
		cout << u - d << endl;
	}
	return 0;
}