require 'prime'
a = Prime.each(10_000_000).each_cons(4).map {|x, _, _, y| y - x== 8 ? y : nil}.compact
loop do
    n = gets.to_i
    break if n == 0
    i = 0
    i += 1 until a[i] > n
    puts a[i - 1]
end
