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>
using namespace std;
int main(){
  int isPrime[1000001];
  fill(isPrime,isPrime + 1000001,1); 
  isPrime[0]=isPrime[1]=0;
  for(int i = 2 ; i*i < 1000001 ; i++ ){
    if(isPrime[i])for(int j = i*2 ; j < 1000001 ; j+=i )isPrime[j] = 0;
  }
  int n;
  while(cin >> n,n){
    int cnt = 0;
    int p,q;
    for(int i = n ; ; i++){
      if(isPrime[i] == 1){
	p = i;
	break;
      }
    }
    int f = 1;
    for(int i = n ; ; i--){
      if(i < 0){
	f = 0;
	break;
      }
      if(isPrime[i] == 1){
	q = i;
	break;
      }
    }
    if(f == 0)cout << 0 << endl;
    else cout << p-q << endl;
  }
  return 0;
}

C++: #include <iostream>

using namespace std;

int main(){
	const int maxP = 1299710;
	bool pr[maxP+1];
	for(int i=2;i<=maxP;i++) pr[i] = true;
	for(int i=2;i<=maxP;i++){
		if(!pr[i]) continue;
		for(int j=2*i;j<=maxP;j+=i)
			pr[j] = false;
	}
	int p;
	while(cin >> p, p){
		int L = p, R = p;
		while(!pr[L]) L--;
		while(!pr[R]) R++;
		printf("%d\n", R-L);
	}
}

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

#define MAX 1299809

int main(){

	  vector<int> pri;
	  bool is_p[MAX]={};
	  for(int i=2; i<MAX; i++) is_p[i] = true;
	  for(int i=2; i<MAX; i++){
			if( is_p[i] ) {
				  pri.push_back(i);
				  for(int j=2; i*j<MAX; j++)
						is_p[i*j] = false;
			}
	  }
	  int n;
	  while(cin >> n,n){
			
			int index = lower_bound(pri.begin(), pri.end(), n) - pri.begin();
			if( n == pri[index])
				  cout << 0 << endl;
			else
				  cout << pri[index] - pri[index-1]  << endl;
			
	  }
			
}

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

using namespace std;

typedef long long int lli;
const lli mx = 1299711;

int main()
{
  lli n, s, t;
  bool a[mx];
  fill(a, a+mx, true);
  a[0] = a[1] = false;

  for(lli i = 2; i < mx; i++){
    if(a[i]){
      for(lli j = i+i; j < mx; j += i){
	a[j] = false;
      }
    }
  }

  while(cin>>n && n){
    for(lli i = n; i >= 2; i--){
      if(a[i]){
	s = i;
	break;
      }
    }
    for(lli i = n; i < mx; i++){
      if(a[i]){
	t = i;
	break;
      }
    }

    cout << (t-s) << endl;
  }

  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
using namespace std;
bool f(int a)
{
	if(a<2)return 0;
	for(int i=2;i*i<=a;i++)if(a%i<1)return 0;
	return 1;
}
int n;
main()
{
	while(cin>>n,n)
	{
		if(n==1)
		{
			cout<<0<<endl;
			continue;
		}
		int r=n,l=n;
		while(!f(r))r--;
		while(!f(l))l++;
		cout<<l-r<<endl;
	}
}

C++: #include <stdio.h>
#include <cctype>
#include <limits.h>
#include <math.h>
#include <complex>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16> >(x);
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)
#define if_range(x, y, w, h) if (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))

const int INF = 1000000000;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4]={0, 1, 0, -1}, dy[4]={-1, 0, 1, 0};
using namespace std;
//typedef pair<int, int> P;
struct P {
	int x, y;
	P(int x, int y):x(x), y(y){}
	P(){}
};

/** Problem1276 : PrimeGap **/
const int MAX = 1299710;

int main()
{
	vector<bool> isPrime(MAX);
	fill(isPrime.begin(), isPrime.end(), true);
	isPrime[0] = isPrime[1] = false;
	isPrime[2] = true;
	for (int i=2; i*i <= MAX; i++) {
		if (!isPrime[i]) continue;
		for (int j=2; j*i<=MAX; j++)
			isPrime[i*j]=false;
	}
	
	int N;
	while (cin>>N, N) {
		if (isPrime[N]) {
			cout << 0 << endl;
			continue;
		}
		
		int a=0, b=0;
		rep(i, MAX) {
			if (isPrime[N-i]) {
				a = N-i;
				break;
			}
		}
		
		rep(i, MAX) {
			if (isPrime[N+i]) {
				b = N+i;
				break;
			}
		}
		
		cout << b-a << endl;
	}
}

C++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
// #define main int main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vpii = vector<pii>;
using vvi = vector<vi>;
using vvb = vector<vb>;
using vvpii = vector<vpii>;
template<typename A> using fn = function<A>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int &n) { return n & 1; }
constexpr bool even(const int &n) { return !odd(n); }
void solve();
main { solve(); return 0; }
// clang-format on

constexpr bool isPrime(const int &n) {
  if (n == 1) return false;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) return false;
  }
  return true;
}

void solve() {
  int k;
  while (cin >> k, k) {
    int l = k, r = k;
    while (!isPrime(l)) l--;
    while (!isPrime(r)) r++;
    cout << r - l << endl;
  }
}