require 'prime'
if true then
  fh=STDIN
else
  fh=open("in0222.txt")
end

ns=[]
while true
  n=fh.gets.chomp.to_i
  break if n==0
  ns.push n
end

nmax=ns.max

p1=Prime.each(nmax).to_a.reverse.each_cons(4)
#fh=open("xxx")
ns.each{|m|
  p1.find{|x,y,z,u| y==x-2 and z==x-6 and u==x-8}.first
}