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 <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(),(c).end()
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define iter(c) __typeof((c).begin())
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
#define pb(a) push_back(a)
#define pr(a) cout<<(a)<<endl
#define PR(a,b) cout<<(a)<<" "<<(b)<<endl
#define F first
#define S second
#define ll long long
bool check(int n,int m,int x,int y){return (x<0||x>=n||y<0||y>=m)?false:true;}
const ll MAX=1000000007,MAXL=1LL<<60,dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
typedef pair<int,int> P;

vector<int> sieve(int n) {
  vector<int> p(n);
  REP(i,2,n) p[i]=i;
  for(int i=2; i*i<n; i++) {
    if(p[i]) {
      for(int j=i*i; j<n; j+=i) p[j]=0;
    }
  }
  p.erase(remove(all(p),0),p.end());
  return p;
}

int main() {
  vector<int> p=sieve(2000000);
  int n;
  while(cin >> n && n) {
    int i=0;
    while(p[i]<n) i++;    if(p[i]==n) pr(0);
    else pr(p[i]-p[i-1]);
  }
  return 0;
}

C++: #include <stdio.h>
#include <algorithm>
#include <iostream>
#include <stack>
#include <math.h>

using namespace std;

#define NUM 1300000

int main(){
	int* table = new int[NUM];
	int limit;

	for(int i=0; i < NUM;i++)table[i] = 1;
	table[0] = 0;
	table[1] = 0;

	limit = sqrt(NUM);

	for(int i=2;i<=limit;i++){
		if(table[i] == 1){
			for(int k=2*i;k < NUM; k += i){
				table[k] = 0;
			}
		}
	}

	table[1] = 1;
	table[2] = 0;

	int pre_index = 2,gap;

    for(int i = 3; i <= 1299709; i++){
		if(table[i] == 1){
			table[i] = 0;
			gap = i - pre_index;
			for(int k = i-1; k >= pre_index+1;k--){
				table[k] = gap;
			}
			pre_index = i;
		}
	}

	int n;

	while(true){
		scanf("%d",&n);
		if(n == 0)break;

		printf("%d\n",table[n]);
	}
	return 0;
}

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

using namespace std;

int main()
{
	int n, s, t;
	bool a[1000001];
	
	fill(a, a+1000001, true);
	a[0] = a[1] = false;
	
	for(int i = 2; i <= 1000000; i++){
		if(a[i]){
			for(int j = 2*i; j <= 1000000; j+=i){
				a[j] = false;
			}
		}
	}
	
	while(1){
		cin >> n;
		if(n == 0){
			break;
		}
		if(a[n]){
			cout << 0 << endl;
			continue;
		}
		s = t = 0;
		
		for(int i = n-1; i >= 0; i--){
			if(a[i]){
				s = i;
				break;
			}
		}
		
		for(int i = n+1; i <= 1000000; i++){
			if(a[i]){
				t = i;
				break;
			}
		}
		
		cout << (t-s) << endl;
	}
	
	return 0;
}

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

using namespace std;

const int MAX = 1299710;
bool prime[MAX];
vector<int> V;

void make(){
  fill(prime,prime+MAX, 1);
  prime[0] = prime[1] = 0;
  for(int i = 2; i < sqrt(MAX); i++)
    if(prime[i]) for(int j = 2*i; j < MAX; j+=i) prime[j] = 0;

  for(int i = 2; i < MAX; i++) 
    if(prime[i]) V.push_back(i);
}
int main(){
  make();
  vector<int>::iterator ite;
  int in;
  while(cin >> in && in){
    ite = upper_bound(V.begin(),V.end(),in);
    if(!prime[in])cout << *ite-*(ite-1) << endl;
    else cout << "0" << endl;
  }
  return 0;
}

C++: #include <cstdio>

bool num[2000000];

void prime(){
	num[0] = false;
	num[1] = false;
	for(int i = 2; i < 2000000; i++) num[i] = true;
	for(int i = 2; i < 2000; i++){
		if(!num[i]) continue;
		for(int j = 2*i; j < 2000000; j += i){
			num[j] = false;
		}
	}
}

int main(){
	prime();
	int n;
	while(scanf("%d", &n),n){
		if(num[n]){
			printf("0\n");
			continue;
		}
		int cnt = 0;
		for(int i = 1;;i++){
			if(num[n+i]){
				cnt += i;
				break;
			}
		}
		for(int i = 1;;i++){
			if(num[n-i]){
				cnt += i;
				break;
			}
		}
		printf("%d\n", cnt);
	}
}

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

bool a[1300000] = { false };

int main(){
	for (int i = 2; i < 1300000; i++) {
		if (!a[i]) {
			for (int j = i * 2; j < 1300000; j += i) {
				a[j] = true;
			}
		}
	}
	int k, f, l;
	while (cin >> k && k != 0) {
		for (int i = k;;i++) {
			if (!a[i]) {
				l = i;
				break;
			}
		}
		for (int i = k;;i--) {
			if (!a[i]) {
				f = i;
				break;
			}
		}
		cout << l - f << endl;
	}

	return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS // #pragma warning(disable:4996)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <sstream>
#include <cmath>
#include <set>
#include <map>
#include <stack>
using namespace std; 

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(a);i>=(b);i--)
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define all(a) a.begin(),a.end()
#define len(x) ((int)(x).size())
#define tmax(a,b,c) max((a),max((b),(c)))
#define tmin(a,b,c) min((a),min((b),(c)))
#define debug(x) cerr << #x << " is " << x << endl;

typedef pair<int, int> Pii;
typedef map<int, int> Mii;
typedef vector<int> Vi;
typedef long long ll;
const int inf = 2e9;
const ll ll_inf = 1e17;
const int mod = 1e9 + 7;
const long double eps = 1e-10;

int p[1322501];

int main()
{
	rep(i,2,1150) {
		int j = i;
		while (j < 1322500) {
			j += i;
			p[j]++;
		}
	}

	while (1) {
		int n; cin>>n;
		if (!n) break;
		if (!p[n]) cout << 0 << endl;
		else {
			int l=n, r=n;
			while (p[l]) l--;
			while (p[r]) r++;
			cout << r-l << endl;
		}
	}
	return 0;
}