require 'prime'

### constant

MAX_N = 1299709

### subroutines

def lower_bound(arr, key, i0 = 0, i1 = arr.length)
  lb = i0 - 1
  ub = i1

  while lb + 1 < ub
    m = (lb + ub) / 2
    if arr[m] >= key
      ub = m
    else
      lb = m
    end
  end
  ub
end

### main

pnums = Prime.each(MAX_N).to_a

loop do
  n = gets.to_i
  break if n == 0

  i = lower_bound(pnums, n)

  if i == 0 || n == pnums[i]
    puts 0
  else
    puts pnums[i] - pnums[i - 1]
  end
end