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<cmath>
#include<vector>

using namespace std;
const int n_max = 1299710;
int prime[n_max];
int main(){
	
	for(int i = 0;i < n_max;i++){
		prime[i] = true;
	}
	
	for(int i = 2;i < sqrt(n_max);i++){
		if(prime[i]){
			for(int j = 0; i * (j + 2) < n_max;j++){
				prime[i*(j+2)] = false;
			}
		}
	}
	
	vector<int> ans;
	while(true){
		int n;
		cin >> n;
		if(n == 0) break;
		if(prime[n]){
			ans.push_back(0);
		}else{
			int pl=0,pr=0;
			for(int i = 1;i < n_max;i++){
				if(prime[n+i] && pr == 0){
					pr = n+i;
				}
				if(prime[n-i] && pl == 0){
					pl = n-i;
				}
				if(pr != 0 && pl != 0){
					ans.push_back(pr - pl);
					break;
				}
			}
		}
	}
	
	for(int i = 0;i < ans.size();i++){
		cout << ans[i] << endl;
	}
	
	return 0;
}

C++: #include "bits/stdc++.h"

#define REP(i,n) for(ll i=0;i<n;++i)
#define RREP(i,n) for(ll i=n-1;i>=0;--i)
#define FOR(i,m,n) for(ll i=m;i<n;++i)
#define RFOR(i,m,n) for(ll i=n-1;i>=m;--i)
#define ALL(v) (v).begin(),(v).end()
#define PB(a) push_back(a)
#define UNIQUE(v) v.erase(unique(ALL(v)),v.end());
#define DUMP(v) REP(i, (v).size()) { cout << v[i]; if (i != v.size() - 1)cout << " "; else cout << endl; }
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

const int dx[8] = { 1,1,0,-1,-1,-1,0,1 };
const int dy[8] = { 0,1,1,1,0,-1,-1,-1 };


using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
///(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)///
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	const int s = 1299710;
	vector<bool> prime(s, true);
	prime[0] = false; prime[1] = false;
	REP(i, s) {
		if (prime[i]) {
			for (int j =2; i*j < s; ++j)prime[i*j] = false;
		}
	}

	while (1) {
		int n;
		cin >> n;
		if (!n)break;
		int cnt1 = 0, cnt2 = 0;
		FOR(i, n, s) {
			if (!prime[i])cnt1++;
			else break;
		}
		RFOR(i, 0, n) {
			if (!prime[i])cnt2++;
			else break;
		}
		if (prime[n])cout << 0 << endl;
		else cout << cnt1 + cnt2 + 1 << endl;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
int prime[10000];
int main(){
	int count=0;
	int i=2;
	while(true){
		bool t=true;
		for(int j=0;j<count;j++){
			if(i%prime[j]==0)
				t=false;
		}
		if(t==true){
			prime[count]=i;
			count++;
		}
		i++;
		if(i==10000)
			break;
	}
	int n;
	while(true){
	cin>>n;
	if(n==0)break;
	int bef,aft;
	bef=n;
	while(true){
		bool t=true;
		for(int i=0;i<count;i++){
			if(bef%prime[i]==0&&bef!=prime[i])
				t=false;
		}
		if(t==true)
			break;
		bef--;
	}
	aft=n;
	while(true){
		bool t=true;
		for(int i=0;i<count;i++){
			if(aft%prime[i]==0&&aft!=prime[i])
				t=false;
		}
		if(t==true)
			break;
		aft++;
	}
	cout<<aft-bef<<endl;
	}
	return 0;
}

C++: //suhan lee,saitama university

#include <vector>
#include <list>
#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 <cstring>
#include <ctime>
#include <climits>
#include <cassert>
#include <complex>
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 EXIST(s,e) ((s).find(e)!=(s).end())
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define repn(i,n) for(int i=0;i<n;i++)
#define EACH(t,i,c) for(t::iterator i=(c).begin(); i!=(c).end(); ++i)
#define pb push_back 
#define pf push_front

int main(){
  bool prime[1300000];
  int tmp,tmp2;
  int k;
  for(int i=0;i<1300000;i++)
    prime[i]=0;
  for(int i=2;i<1300000;i++){
    if(!prime[i]){
      tmp=i;
      //  cout <<i<<endl;
	tmp+=i;
      while(tmp<1300000){

	prime[tmp]=1;
	tmp+=i;	
      }
    }
  }
      while(1){
	cin>>k;
	if(k==0) return 0;
	if(!prime[k])
	  cout<<"0\n";
	else{
	  tmp2=k;
	  tmp=k;
	  while(prime[tmp2])
	    tmp2--; 
	  while(prime[tmp])
	    tmp++; 
	  //	  cout<<tmp<<" "<<tmp2<<" "<<tmp-tmp2<<endl;
	  cout<<tmp-tmp2<<endl;
	}

      }

}

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

typedef long long ll;

vector<ll> prime;

int main(){
  prime.push_back(2);
  for(ll i=3; i<1200000; i+=2){
    bool flag=true;
    for(ll j=0; j<prime.size() && i>=prime[j]*prime[j]; j++){
      if(i%prime[j]==0){flag=false; break;}
    }
    if(flag)prime.push_back(i);
  }
  while(true){
    int n;
    cin>>n;
    if(n==0)break;
    ll to;
    for(to=0; n>prime[to]; to++);
    if(n==prime[to])cout<<"0"<<endl;
    else cout<<prime[to]-prime[to-1]<<endl;
  }
  return 0;
}