max = 1e7
max2 = (max-3)/2
sieve = Array.new(max2, true)
for i in 0 .. Integer((Math.sqrt(max)-3)/2)
  next unless sieve[i]
  k = i+i+3
  j = k*(i+1)+i
  while j <= max2
    sieve[j] = nil
    j += k
  end
end
p [*0..($_.to_i-3)/2].rindex{|i|(sieve[i-1,2]+sieve[i-4,2]).all?}*2+3while gets>?0