main = interact $ unlines . map (show . prime_gap . read) . init . lines

prime_gap n = if p == n then 0 else p - (last ps1)
 where (ps1,(p:_)) = span (<n) primes

primes = 2:3:[x | i<-[1..], j<-[-1,1], let x = 6*i+j, isPrime x]
 where isPrime n = null [i | i<-takeWhile (\x -> x*x <= n) primes, rem n i == 0]