require "prime" require "set" prime_max = 5000000 gets set = gets.split.map(&:to_i).to_set ok = Set.new start = 1 remain = set exist_flag = false complete_flag = false Prime.each(prime_max) do |prime| nums = prime.to_s.each_char.map(&:to_i).to_set if set.superset?(nums) unless complete_flag exist_flag = true remain -= nums if remain.empty? complete_flag = true end end else if exist_flag if complete_flag ok << prime - 1 - start end remain = set end start = prime + 1 end end if complete_flag ok << prime_max - start end if ok.empty? puts(-1) else puts(ok.max) end