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++: 
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1276

#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<cmath>
#include<climits>
#include<ctime>
#include<cstring>
#include<numeric>

#define ALL(v) (v).begin(),(v).end()
#define REP(i,p,n) for(int i=p;i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list) cout << "{ "; for(auto nth : list){ cout << nth << " "; } cout << "}" << endl;

#define PRIME_MAX 1300000

using namespace std;

int main()
{
	int K;
	bool is_prime[PRIME_MAX];
	is_prime[0] = false;
	is_prime[1] = false;
	REP(i,2,PRIME_MAX) is_prime[i] = true;
	REP(i,2,PRIME_MAX) {
		if (is_prime[i]) {
				for (int j=i+i; j<PRIME_MAX; j+=i) is_prime[j] = false;
		}	
	}

	while(cin >> K && K)
	{
		int left_prime, right_prime;
		for(int lt=K; lt>0; --lt)
		{
			if(is_prime[lt]){ 
				left_prime = lt;
				break;
			}
		}

		for(int rt=K; rt<PRIME_MAX; ++rt)
		{
			if(is_prime[rt]){
				right_prime = rt;
				break;
			}
		}

		cout << right_prime - left_prime << endl;
	}
	return 0;
}

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

int main ()
{
    vector<bool> num(2222222, true);
    num[0] = num[1] = false;
    vector<int> prim;
    for (int i = 2; i <= 2222222; i++) {
        for (int j = 2; num[i] && j * i <= 2222222; j++) {
            num[i * j] = false;
        }
        if (num[i]) prim.push_back(i);
    }
    int n;
    while (cin >> n, n) {
        auto i = lower_bound(prim.begin(), prim.end(), n) - prim.begin();

        int res = 0;
        if (n < prim[i]) res = prim[i] - prim[i-1];
        if (n > prim[i]) res = prim[i+1] - prim[i];

        cout << res << endl;
    }

    return 0;
}

C++: //22
#include<iostream>

using namespace std;

#define MAX 1299709

int main(){
  static bool cmp[MAX+1]={true,true};
  for(int i=2;i<=MAX;i++){
    if(!cmp[i]){
      for(int j=2;j*i<=MAX;j++){
	cmp[i*j]=true;
      }
    }
  }
  for(int n;cin>>n,n;){
    if(!cmp[n]){
      cout<<0<<endl;
    }else{
      int f=n,l=n;
      while(cmp[f]){
	f--;
      }
      while(cmp[l]){
	l++;
      }
      cout<<l-f<<endl;
    }
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
int isPrime(int n){
  for(int i=2;i*i<=n;i++) if(n%i==0) return 0;
  return 1;
}
int main(){
  int i,j,dp[1500000]={},c;
  i=2;
  
  while(i<1500000){
    c=1;
    dp[i]=0;
    while(isPrime(i+c)==0){
      c++;
    }
    if(i+c<1500000){
      for(j=i+1;j<i+c;j++) dp[j]=c;
      dp[i+c]=0;
    }
    i+=c;
  }
  int n;
  cin >> n;
  while(n!=0){
    cout << dp[n] << endl;
    cin >> n;
  }
  return 0;
}

C++: #include<iostream>
#define rep(i, n) for(int i = 0; i < (n); i++)
using namespace std;
bool prime[1300000] = {0};

int main(){
  prime[0] = prime[1] = 1;
  rep(i, 1300000){
    if(prime[i])continue;
    for(int j = i+i; j < 1300000; j+=i)prime[j]=1;
  }
  int n;
  while(cin>>n, n){
    if(!prime[n]){
      cout<<0<<endl;
      continue;
    }
    int mx, mn;
    for(mx = n; prime[mx]; mx++);
    for(mn = n; prime[mn]; mn--);
    cout<<mx-mn<<endl;
  }
  return 0;
}

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

int main(){
    bool nonp[1500000] = {};
    vector<int> p;
    for(int i = 2; i < 1500000; i++){
        if(!nonp[i]){
            p.push_back(i);
            for(int j = i+i; j < 1500000; j += i)   nonp[j] = true;
        }
    }

    int n;
    while(cin >> n, n){
        if(!nonp[n]){
            cout << 0 << endl;
        }else{
            auto it = upper_bound(p.begin(), p.end(), n);
            cout << *it - *(it-1) << endl;
        }
    }

    return 0;
}

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

using namespace std;

int main()
{
  int isPrime[1500000];
  for(int i = 0; i < 1500000; i++) isPrime[i] = true;
  isPrime[0] = isPrime[1] = false;
  for(int i = 2; i < 1500000; i++){
    if(isPrime[i]){
      for(int j = i+i; j < 1500000; j += i) isPrime[j] = false;
    }
  }
  int k;
  while(cin >> k, k){
    int lw, up;
    for(int i = k; i > 1; i--){
      if(isPrime[i]){
	lw = i;
	break;
      }
    }
    for(int i = k; i < 1500000; i++){
      if(isPrime[i]){
	up = i;
	break;
      }
    }
    cout << up - lw << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define each(a, b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define pb push_back
#define show(x) cout <<#x<<" = "<<(x)<<endl
#define spair(p) cout <<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(i,v.size())cout<<" "<<v[i];cout<<endl
#define sset(s) cout<<#s<<":";each(i,s)cout <<" "<<i;cout<<endl

using namespace std;

typedef pair<int,int>P;

const int MAX_N = 100005;

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

int sieve(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;
			}
		}
	}
	return p;
}


int main()
{
    sieve(1299709);
    while(1){
        int n;
        cin >> n;
        if(n == 0){
            break;
        }
        if(is_prime[n]){
            cout << "0\n";
            continue;
        }
        int i = 0;
        while(prime[i] < n){
            i++;
        }
        cout << prime[i] - prime[i-1] << "\n";
    }
}